简体   繁体   中英

Why does WooCommerce update order status when I use redirect in woocommerce_thankyou hook?

I am using Custom Payment Gateway plugin to create two payment options 1) via an external site for products that require upfront payment and 2) an onsite solution that just collects payment details for subscription products.

Within the plugin I have set the default order status for both to ' pending payment '. For the external payment option I am implementing a redirect to the external payment site and then sending the user back to the main site (a custom payment confirmation page) upon successful payment using the order key to update the order status.

I am using the woocommerce_thankyou hook below to do some api calls and then creating a redirect if the users needs to pay via the external site:

    add_action( 'woocommerce_thankyou', 'payment_options', 20, 1);
    function payment_options($order_id) { 
      //Do API calls etc then...
      if( $paymentMethodId == 203 ) {
        wp_safe_redirect( '/redirect/' );//Redirect page collects data and forwards to external site    
        exit;
      }
    }

This works fine if I comment out the redirect - the thank you page displays the correct information and the status remains as 'pending payment' until I update manually update it.

However, if I do the redirect the order status updates to 'processing', immediately, for no explicable reason before the user has made payment or left the external site (I have not set this anyway in the code-base) and there is no setting elsewhere in WooCommerce I can see. It was my understanding that this is controlled purely by the payment method used which is set correctly?

Any ideas as to what might be causing this status update?

add_action('template_redirect', 'wc_custom_redirect_after_purchase');

function wc_custom_redirect_after_purchase() {
    global $wp;

    if (is_checkout() && !empty($wp->query_vars['order-received'])) {

        $order = new WC_Order($wp->query_vars['order-received']);

        $payment_method = $order->get_payment_method();
        if ( 203 == $payment_method ) {
            wp_safe_redirect('/redirect/'); //Redirect page collects data and forwards to external site    
        }
    }

    exit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM