订单中有缺货商品时更改订单状态
问题描述:
在WooCommerce中,如果此订单中有后退订购商品,如何将on-hold
订单状态更改为其他商品?订单中有缺货商品时更改订单状态
我试图使用一个自定义函数挂在woocommerce_order_status_on-hold
行动挂钩没有成功。
任何人都可以帮助我解决这个问题吗?
谢谢。
答
function mysite_hold($order_id) {
$order = new WC_Order($order_id);
$items = $order->get_items();
$backorder = FALSE;
foreach ($items as $item) {
if ($item['Backordered']) {
$backorder = TRUE;
break;
}
}
if($backorder){
$order->update_status('completed'); //change your status here
}
}
add_action('woocommerce_order_status_on-hold', 'mysite_hold');
//You may need to store your backorder info like below
wc_add_order_item_meta($item_id, 'Backordered', $qty - max(0, $product->get_total_stock()));
请试试这个片断
答
Updated compatibility for woocommerce 3+
这里是woocommerce_thankyou
动作钩子钩住的自定义功能,这将改变订单状态如果这个顺序有“待机”状态和如果它有任何缺货产品。
您将有设置在功能所需的新状态slug更改。
这里是一个自定义函数(代码有很好的注释):
add_action('woocommerce_thankyou', 'change_paid_backorders_status', 10, 1);
function change_paid_backorders_status($order_id) {
if (! $order_id)
return;
// HERE set your new status SLUG for paid back orders <== <== <== <== <== <== <==
$new_status = 'completed';
// Get a an instance of order object
$order = wc_get_order($order_id);
// ONLY for "on-hold" ORDERS Status
if (! $order->has_status('on-hold'))
return;
// Iterating through each item in the order
foreach ($order->get_items() as $item_values) {
// Get a an instance of product object related to the order item
product = version_compare(WC_VERSION, '3.0', '<') ? wc_get_product($item_values['product_id']); : $cart_item->get_product();
// Check if the product is on backorder
if($product->is_on_backorder()){
// Change this order status
$order->update_status($new_status);
break; // Stop the loop
}
}
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
该代码已经过测试和工作。
感谢您的完美工作 但此行显示错误: wc_add_order_item_meta($ item_id,'Backordered',$ qty - max(0,$ product-> get_total_stock())); –
如何获得woocommerce的订单详情? 如果订单包含销售价格,那么我想更改订单状态 –