WooCommerce order status change from payment gateway

送分小仙女□ 提交于 2019-12-08 01:50:46

问题


I have integrated a payment gateway to accept online payments for my store running on woocommerce. Everything works fine but I noticed that woocommerce is changing the order status to wc-processing for all the online paid orders by default.

As per my store's functionality I want all the online paid orders to be in wc-on-hold status initially.

Is there any way to stop woocommerce changing the order status to wc-processing programatically?


回答1:


yes there is a way, but you need to modify the payment plugin or add your own code, you can read this to understand how payments work.

Now, woocommerce use $order->payment_complete() method to handle the completed order, so you need to hook your own function to modify the status, here is the description of that method

Use this filter: woocommerce_payment_complete_order_status




回答2:


Here it is a code snippet based on this thread. We use here woocommerce_thankyou (that is fired just after payment has been done) to hook our function, converting 'processing' orders status to 'on-hold':

add_action( 'woocommerce_thankyou', 'custom_woocommerce_paid_order_status', 10, 1 );
function custom_woocommerce_paid_order_status( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    global $woocommerce;
    $order = new WC_Order( $order_id );

    // 'processing' orders status are converted to 'on-hold'.
    if ( is_object($order) && $order->has_status( 'processing' ) {
        $order->update_status( 'on-hold' ); 
    }

    return;
}

You can also target in your conditions the payment gateways for example here we bypass 3 payment gateways and target a specific payment gateway using "your_payment_gateway" slug:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_paid_order_status', 10, 1 );
function custom_woocommerce_paid_order_status( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    global $woocommerce;
    $order = new WC_Order( $order_id );

    // Bypass orders with Bank wire, Cash on delivery and Cheque payment methods.
    if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
        return;
    }

    // Target your "your_payment_gateway_slug" with this conditional
    if ( is_object($order) && get_post_meta($order->id, '_payment_method', true) == 'your_payment_gateway_slug'  && $order->has_status( 'processing' ) ) {
        $order->update_status( 'on-hold' ); 
    }

    return;
}

This code snippets goes on function.php file of your active child theme or theme.

You can easily do anything you want, and the correct hook for paid orders is woocommerce_thankyou

References:

  • WooCommerce Class WC_Abstract_Order
  • WooCommerce: Auto complete paid Orders (depending on Payment methods)
  • Renaming WooCommerce Order Status


来源:https://stackoverflow.com/questions/38741747/woocommerce-order-status-change-from-payment-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!