Change Woocommerce Order Status based on Shipping Method

北城余情 提交于 2019-12-04 18:34:47

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_id(), $search ) !== false ){
            $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…

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