I'm having an issue that's driving me absolutely insane with the payment complete hook because it works perfectly with the PayPal Sandbox, but not when I go live.
I require all new customers to verify the PayPal email used with their first purchase. I believe the problem I'm having is that the hook and function is firing off before WordPress has written the paypal email to the post meta.
I can't post the whole function, but here's what's relevant:
add_action('woocommerce_payment_complete', 'preapproved_api_order', 10, 1);
function preapproved_api_order($order_id){
if ( ! empty( $gateway->id ) && 'paypal' == $gateway->id ) {
$order = new WC_Order( $order_id );
$user_id = $order->get_user_id();
$gateway = wc_get_payment_gateway_by_order( $order );
$paypal_email = get_post_meta( $order_id, 'Payer PayPal address', true );
$error = 'paypal email used for payment '.$paypal_email;
send_test_mail($error);
}
I added some quick error checking and that email does get sent without an email address included, which is why I'm thinking WP hasn't written the email address to the post meta.
If anyone can think of an alternative way to get the paypal email address so that it works with this hook it would be greatly appreciated. Very strange that it works in the sandbox and not live, I'm not sure what can be causing the delay. Thanks in advance.
I couldn't figure out what was causing the delay posting to the post_meta, but I did find the IPN hook that captures all the order and payment details being sent back from PayPal. Just an alternative way for a payment complete hook for PayPal in WooCommerce. Hopefully it helps others experiencing the same issue:
add_action('valid-paypal-standard-ipn-request', 'preapproved_api_order', 10, 1);
function preapproved_api_order($details){
global $woocommerce, $wpdb;
$order_id = $details['item_number1'];
$order = new WC_Order( $order_id );
}