2017-03-03 97 views
1

在WooCommerce中,如果此訂單中有後退訂購商品,如何將on-hold訂單狀態更改爲其他商品?訂單中有缺貨商品時更改訂單狀態

我試圖使用一個自定義函數掛在woocommerce_order_status_on-hold行動掛鉤沒有成功。

任何人都可以幫助我解決這個問題嗎?

謝謝。

回答

0
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())); 

請試試這個片斷

+0

感謝您的完美工作 但此行顯示錯誤: wc_add_order_item_meta($ item_id,'Backordered',$ qty - max(0,$ product-> get_total_stock())); –

+0

如何獲得woocommerce的訂單詳情? 如果訂單包含銷售價格,那麼我想更改訂單狀態 –

2

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文件或也以任何插件文件。

該代碼已經過測試和工作。