根据运输方式更改 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.
因为我有一些不同的快递"运输方式费率,我认为通过使用 stristr()
来查看单词 'express'
是否出现在格式化的运输方式标题.但我似乎错过了一些东西,因为我什么也没得到.
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. 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?
这是我拥有的代码:
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-----------------------------------------------------------
对于使用 Woocommerce Table Rate Shipping 的任何人,get_method_id 返回表速率 ID,所以我使用 get_method_title 代替,如下所示,如果有更好的方法请评论...
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...
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;
}
}
}
推荐答案
我更喜欢使用速度更快、内存占用更少的函数 strpos()
作为发货方式ID 总是小写的(就像一种 slug).
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).
所以最好获得 WC_Order_Item_Shipping代码>
本案例的对象数据,使用可用的方法.
So is better the get the WC_Order_Item_Shipping
object data for this case, using the available methods.
所以代码应该是:
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;
}
}
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
经过测试并有效......
Tested and works…
这篇关于根据运输方式更改 Woocommerce 订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!