简体   繁体   English

根据运输方式更改 Woocommerce 订单状态

[英]Change Woocommerce Order Status based on Shipping Method

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 "express delivery" Shipping Method rates I thought that by using stristr() to see if the word 'express' appears anywhere in the formatted shipping method title.因为我有一些不同的“快递”运输方式费率,我认为通过使用stristr()来查看'express'一词是否出现在格式化的运输方式标题中的任何地方。 But I seem to be missing something as I don't get anything.但我似乎错过了什么,因为我什么也没得到。

How can I check if the Order shipping method is an "express delivery" to be able to update the order status?如何检查订单送货方式是否为“快递”以便能够更新订单状态?

Here is the code that I have:这是我的代码:

add_action( 'woocommerce_thankyou', 'express_orders_4865', 10, 1 );
function express_orders_4865( $order_id ) {
    global $woocommerce;

    $order = new WC_Order( $order_id );

    $shipping_method = $order->get_shipping_method();

    if (stristr($shipping_method, 'express') === TRUE) {
        $order->update_status('on-hold');
    } else {
        return;
    }
}

EDIT-----------------------------------------------------------编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ----------

For anyone using Woocommerce Table Rate Shipping the get_method_id returns the table rate id so i used get_method_title instead as below, if there is a better way please comment...对于使用 Woocommerce Table Rate Shipping 的任何人,get_method_id 返回表格费率 ID,所以我使用 get_method_title 代替如下,如果有更好的方法请评论...

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');
            break;
        }
    }
}

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).我更喜欢使用速度更快、内存占用更少的函数strpos()代替,因为运输方法 ID 总是小写(就像一种 slug)。

So is better the get the WC_Order_Item_Shipping object data for this case, using the available methods.因此,最好使用可用方法获取这种情况下的WC_Order_Item_Shipping对象数据。

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.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

Tested and works…经过测试并有效……

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所以上面的代码对我不起作用,我有一个 FB 组的人帮我调试它,这是最后一个对我有用的

 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; } } }

My solution assumes that the normal status for a new order is PROCESSING.我的解决方案假设新订单的正常状态是处理中。

So when an order is changed to PROCESSING, check the shipping method and if it matches, then change it to ON-HOLD.因此,当订单更改为 PROCESSING 时,请检查运输方式,如果匹配,则将其更改为 ON-HOLD。

add_action('woocommerce_order_status_changed', 'jds_auto_change_status_by_shipping_method');
function jds_auto_change_status_by_shipping_method($order_id) {
    // If the status of an order is changed to PROCESSING and the shipping method contains specific text then change the status.
    if ( ! $order_id ) {
        return;
    }
    global $product;
    $order = wc_get_order( $order_id );
    if ($order->data['status'] == 'processing') { // if order status is processing
        $shipping_method = $order->get_shipping_method();
        if ( strpos($shipping_method, 'express') !== false ) { // if shipping method CONTAINS this text
            $order->update_status('on-hold'); // change status to this
        }
    }   
}

Note that $shipping_method returns the human readbale version of the shipping method that the customer sees, so you need to match exactly how the word 'express' appears to customer... is it 'express' or 'Express' or 'EXPRESS'请注意, $shipping_method 返回客户看到的运输方式的人工可读版本,因此您需要准确匹配“快递”一词对客户的显示方式......是“快递”还是“快递”或“快递”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce 根据不同的运输方式更改订单状态 - Change Woocommerce Order Status based on different Shipping Methods 根据单选按钮更改 Woocommerce 发货方式 - change Woocommerce shipping method based on radio buttons 更改 WooCommerce 发货方式 全 label 根据产品发货 class - Change WooCommerce shipping method full label based on product shipping class Woocommerce 根据选择的运输类别在结账时更改运输方式标题 - Woocommerce Change shipping method title on checkout based on shipping class selected 根据不同的运输和付款方式更改 woocommerce 状态 - Change woocommerce status based on different shipping and payment methods WooCommerce 根据特定的运输方式更改 BACS 订单的状态 - WooCommerce change status for BACS orders based on specific shipping methods 根据 WooCommerce 中的运输方式自定义订单接收页面 - Customize Order received page based on shipping method in WooCommerce 高亮 WooCommerce admin 订单列表 based on order shipping method - Highlight WooCommerce admin orders list based on order shipping method 基于 Woocommerce 新订单通知的配送方式 ID 的电子邮件收件人 - Email recipients based on Shipping method Id for Woocommerce new order notification 如何根据送货方式将自定义元数据添加到 WooCommerce 订单 - How to add Custom Meta Data to a WooCommerce Order based on the Shipping Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM