【问题标题】:Change Woocommerce Order Status based on different Shipping Methods根据不同的运输方式更改 Woocommerce 订单状态
【发布时间】:2021-02-27 00:51:16
【问题描述】:

我正在使用Change Woocommerce Order Status based on Shipping Method 代码,它非常适合根据运输方式字符串在 WooCommerce 中重新分配我的自定义订单状态“awaiting-pickup”。

这是我的代码:

add_action( 'woocommerce_thankyou', 'shipping_method_update_order_status', 10, 1 );
    function shipping_method_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
    
        $search = 'local_pickup'; // 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 "pickup" method is used, we change the order to "awaiting-pickup" status
            if( strpos( $shipping_item->get_method_title(), $search ) !== false ){
                $order->update_status('awaiting-pickup');
                $order->save();
                break;
            }
        }
    }

我需要帮助扩展它以应用一些基于其他运输方式的不同规则,例如“free_shipping”和“flat_rate”,我也想重新分配为“等待交付”。

$search = 'flat_rate' OR 'free_shipping';
$order->update_status('awaiting-delivery');

运输实例的结构如下:

'local_pickup:2'
'local_pickup:5'
'local_pickup:7'
'local_pickup:10'

'flat_rate:3'
'flat_rate:6'
'flat_rate:9'

'free_shipping:11'
'free_shipping:12'
'free_shipping:13'

每次我创建一个新的运输区域时,附加到该区域的额外运输实例都会在方法类型上附加新的编号。最终我需要使用以下逻辑的东西:

IF      'local_pickup' IN string
THEN    $order->update_status('awaiting-pickup');
ELSEIF  'flat_rate' OR 'free_shipping' IN string
THEN    $order->update_status('awaiting-delivery');
END

【问题讨论】:

    标签: php wordpress woocommerce status shipping-method


    【解决方案1】:

    更新 2

    由于您在此处使用真实的运输方式 ID,因此您无需搜索字符串。那么让它适用于多种运输方式ID会更简单,如下所示:

    add_action( 'woocommerce_thankyou', 'shipping_method_update_order_status', 10, 1 );
    function shipping_method_update_order_status( $order_id ) {
        if ( ! $order_id ) return;
        
        // Here define your shipping methods Ids
        $shipping_methods_ids_1 = array('local_pickup');
        $shipping_methods_ids_2 = array('flat_rate', 'free_shipping');
    
        // 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 ){
            // For testing to check the shipping method slug (uncomment the line below):
            // echo '<pre>'. print_r( $shipping_item->get_method_id(), true ) . '</pre>';
    
            // When "Local pickup" method is used, we change the order to "awaiting-pickup" status
            if( in_array( $shipping_item->get_method_id(), $shipping_methods_ids_1 ) && ! $order->has_status('awaiting-pickup') ){
                $order->update_status('awaiting-pickup'); // Already use internally save() method
                break; // stop the loop
            }
            // When 'Flat rate' or 'Free shipping' methods are used, we change the order to "awaiting-delivery" status
            elseif( in_array( $shipping_item->get_method_id(), $shipping_methods_ids_2 ) && ! $order->has_status('awaiting-delivery') ){
                $order->update_status('awaiting-delivery'); // Already use internally save() method
                break; // stop the loop
            }
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

    • 感谢您帮助我!我更新了我的问题,以更好地反映我正在处理的问题的一些细节。也就是说,我需要一些东西来查看字符串,因为我们会随着时间的推移继续添加运输区域,因为每种运输类型都有多个编号的实例。此外,我需要一组运输方式来分配“等待取货”的订单状态,而其他两种运输方式分配不同的订单状态“等待交付”。再次感谢您帮助我解决这个问题。
    • 好的,所以我在您编写此代码时尝试了它,但它似乎没有按计划工作。订单确实通过了,但最终处于处理订单状态,并且还为网站产生了严重错误。我注意到以下has_status('awaiting-pickup'),因此我尝试将其更改为has_status('processing'),以防我们将引用的旧状态。在这两种尝试中,由于某种原因,它都不起作用。还有其他可行的想法吗?
    • 当我使用下面的代码foreach($order-&gt;get_shipping_methods() as $shipping_item ){print_r( $shipping_item-&gt;get_method_id());时测试生成“flat_rate”
    • 我遇到了一个问题,用户偶尔会返回感谢屏幕,然后将订单从“已完成”发送回运输桶。为了解决这个问题,我在$order = wc_get_order( $order_id ); 之后添加了一个检查if ($order-&gt;has_status('completed')),以确保一旦订单被标记为已完成,他们就不会返回基于运输方式的订单状态
    猜你喜欢
    • 2018-04-27
    • 2021-02-01
    • 2021-10-26
    • 2022-10-05
    • 2021-04-08
    • 2017-05-24
    • 1970-01-01
    • 2018-08-13
    • 2021-01-30
    相关资源
    最近更新 更多