根据运输方式更改 Woocommerce 订单状态
Change Woocommerce Order Status based on Shipping Method
这里的想法是,当订单以 "express delivery" 作为运输方式时,订单状态将更新为 On-Hold。
因为我有一些不同的 "express delivery" 运输方式费率,我认为通过使用 stristr()
来查看 'express'
一词是否出现在格式化的运输方式标题中的任何地方。但我似乎错过了什么,因为我什么也没得到。
如何检查订单送货方式是否为 "express delivery" 以便能够更新订单状态?
这是我的代码:
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;
}
}
编辑---------------------------------------- --------------
对于任何使用 Woocommerce Table 运费 get_method_id returns table 费率 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;
}
}
}
我更喜欢使用速度更快、占用内存更少的函数 strpos()
,因为送货方式 ID 始终是小写字母(就像一种 slug)。
因此,最好使用可用的方法为这种情况获取 WC_Order_Item_Shipping
对象数据。
所以代码应该是:
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 文件或任何插件文件。
经过测试并有效……
所以上面的代码对我不起作用,我有一个 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;
}
}
}
这里的想法是,当订单以 "express delivery" 作为运输方式时,订单状态将更新为 On-Hold。
因为我有一些不同的 "express delivery" 运输方式费率,我认为通过使用 stristr()
来查看 'express'
一词是否出现在格式化的运输方式标题中的任何地方。但我似乎错过了什么,因为我什么也没得到。
如何检查订单送货方式是否为 "express delivery" 以便能够更新订单状态?
这是我的代码:
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;
}
}
编辑---------------------------------------- --------------
对于任何使用 Woocommerce Table 运费 get_method_id returns table 费率 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;
}
}
}
我更喜欢使用速度更快、占用内存更少的函数 strpos()
,因为送货方式 ID 始终是小写字母(就像一种 slug)。
因此,最好使用可用的方法为这种情况获取 WC_Order_Item_Shipping
对象数据。
所以代码应该是:
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 文件或任何插件文件。
经过测试并有效……
所以上面的代码对我不起作用,我有一个 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;
}
}
}