Change Woocommerce Order Status based on Shipping Method

前端 未结 2 1038
我寻月下人不归
我寻月下人不归 2020-12-10 10:15

The idea here is that when an order comes in with an \"express delivery\" as Shipping Method, the order status is updated to On-Hold.

As there I have some different

相关标签:
2条回答
  • 2020-12-10 10:33

    I prefer to use the faster and less memory intensive function strpos() instead as the shipping method ID is alway in lowercase (like a kind of slug).

    So is better the get the WC_Order_Item_Shipping object data for this case, using the available methods.

    So the code should be:

    add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
    function express_shipping_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
        
        $search = 'express'; // The needle to search in the shipping method ID
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
    
        // Get the WC_Order_Item_Shipping object data
        foreach($order->get_shipping_methods() as $shipping_item ){
            // When "express delivery" method is used, we change the order to "on-hold" status
            if( strpos( $shipping_item->get_method_title(), $search ) !== false && ! $order->has_status('on-hold')){
                $order->update_status('on-hold');
                break;
            }
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works…

    0 讨论(0)
  • 2020-12-10 10:42

    So the code above didn't work for me, i had someone in a FB group help me debug it and this was the final one that worked for me

    add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
    function express_shipping_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
    
        $search = 'express'; // The needle to search in the shipping method ID
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
    
        // Get the WC_Order_Item_Shipping object data
        foreach($order->get_shipping_methods() as $shipping_item ){
            // When "express delivery" method is used, we change the order to "on-hold" status
            if( strpos( $shipping_item->get_method_title(). $search ) !== false ){
                $order->update_status('on-hold');
                $order->save();
                break;
            }
        }
    }

    0 讨论(0)
提交回复
热议问题