2017-07-06 115 views
1

只有當購物車中有缺貨物品(我們的產品可在缺貨時提供)時,我需要在我的結帳訂單審覈中顯示一節。這裏是顯示部分的代碼...檢查購物車中是否有缺貨物品 - WooCommerce

的functions.php:

function notes_in_cart() { 
    global $woocommerce; 
     if (! $_POST || (is_admin() && ! is_ajax())) { 
     return; 
    } 

    if (isset($_POST['post_data'])) { 
     parse_str($_POST['post_data'], $post_data); 
    } else { 
     $post_data = $_POST; // fallback for final checkout (non-ajax) 
    } 
    if (WC()->cart->needs_shipping()){ 
     //if cart has items out of stock 
     //if (cart has out of stock product) { 
     ?> 
      <tr class="ceckoutStockMeta"> 
       <th>Item Shipments</th> 

       <td> 
        <p style="color: red;">*You have one or more items in your cart that are currently out of stock. Please select a custom shipping option for your order.</p><br> 
        <form> 

         <input type="radio" name="stockOp" id="stockOption1" value="ship" /> 
         <label for="stockOption1">Ship what is available now</label><br> 

         <input type="radio" name="stockOp" id="stockOption2" value="hold" /> 
         <label for="stockOption2">Wait and ship together</label> 
        </form> 
       </td> 
      </tr> 

     <?php 

     //} 
    } 
} 
add_action('woocommerce_cart_totals_after_order_total', 'notes_in_cart'); 
add_action('woocommerce_review_order_after_order_total', 'notes_in_cart'); 

眼下部分顯示了所有的時間。我知道我可能需要調用購物車項目並用foreach循環來確定是否缺貨,但不知道如何。

回答

1

您可以這樣做來檢查「庫存數量」和「允許缺貨?」。 - 「使用inStock」或「outofstock」 - 但不會允許

function notes_in_cart() { 
    global $woocommerce; 

    if (! $_POST || (is_admin() && ! is_ajax())) { 
     return; 
    } 

    if (isset($_POST['post_data'])) { 
     parse_str($_POST['post_data'], $post_data); 
    } else { 
     $post_data = $_POST; // fallback for final checkout (non-ajax) 
    } 

    if (WC()->cart->needs_shipping()){ 

     // set $out_of_stock_exists to false by default 
     $out_of_stock_exists = false; 
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
      if($values['data']->backorders_allowed()){ //check if backorders are allowed on this product 
       // get the stock quantity - returns the available amount number 
       $stock_info = $values['data']->get_stock_quantity(); 

       if($stock_info < $values['quantity']){ //thanks to LoicTheAztec for pointing it out in his answer 
        // set $out_of_stock_exists to true and stop foreach execution 
        $out_of_stock_exists = true; 
        break; 
       } 
      } 

     } 

     //if cart has items out of stock 
     if ($out_of_stock_exists) { 
      ?> 
      <tr class="ceckoutStockMeta"> 
       <th>Item Shipments</th> 
       <td> 
        <p style="color: red;">*You have one or more items in your cart that are currently out of stock. Please select a custom shipping option for your order.</p><br> 
        <form> 

         <input type="radio" name="stockOp" id="stockOption1" value="ship" /> 
         <label for="stockOption1">Ship what is available now</label><br> 

         <input type="radio" name="stockOp" id="stockOption2" value="hold" /> 
         <label for="stockOption2">Wait and ship together</label> 
        </form> 
       </td> 
      </tr> 

      <?php 

     } 
    } 
} 
add_action('woocommerce_cart_totals_after_order_total', 'notes_in_cart'); 
add_action('woocommerce_review_order_after_order_total', 'notes_in_cart'); 

它還可以使用get_stock_status()或is_in_stock()返回的「庫存狀態」的值的方法來實現:產品性能https://github.com/woocommerce/woocommerce/issues/11187https://github.com/woocommerce/woocommerce/issues/10834

編輯:爲什麼is_in_stock()不工作的更多細節:使用回單結賬 的is_in_stock()方法是這樣的一個:

public function is_in_stock() { 
    return apply_filters('woocommerce_product_is_in_stock', 'instock' === $this->get_stock_status(), $this); 
} 

這意味着它會檢查「S tock狀態「,如果」庫存「則返回真,如果」缺貨「則返回假。

現在,如果你讀到這裏:https://conschneider.de/manage-stock-backorders-woocommerce/

你會發現:

欠交單隻可能是隻要股票狀態是「 股票」。將產品設置爲缺貨將再次阻止購買 並顯示「缺貨」。

+1

非常好,謝謝!爲我節省了幾個小時。 –

+0

很高興我可以服務 –

1

更新時間:由於所有的產品都可以在缺貨,唯一有效的方法是在foreach檢查庫存量「足夠」每個車項目數量...

如果該產品的庫存數量小於購物車的數量(WC顯示「可在延期交貨」中),則顯示您的「裝運單」。

這裏是代碼:

function notes_in_cart() { 

    if (! $_POST || (is_admin() && is_ajax())){ 
     return; 
    } 

    if (isset($_POST['post_data'])) { 
     parse_str($_POST['post_data'], $post_data); 
    } else { 
     $post_data = $_POST; // fallback for final checkout (non-ajax) 
    } 

    // Loop that check if cart items have enough stock quantity 
    $is_available_on_backorder = false; 
    foreach (WC()->cart->get_cart() as $item_values) { 

     $stock_qty = $item_values['data']->get_stock_quantity(); // Product stock 
     $item_qty = $item_values['quantity']; // Cart Item quantity 

     // Testing if the product has enough stock 
     if($stock_qty < $item_qty){ 
      $is_available_on_backorder = true; // The condition is met 
      break; // we stop the loop 
     } 
    } 

    if (WC()->cart->needs_shipping() && $is_available_on_backorder){ 
     ?> 
      <tr class="ceckoutStockMeta"> 
       <th>Item Shipments</th> 
       <td> 
        <p style="color: red;">*You have one or more items in your cart that are currently out of stock. Please select a custom shipping option for your order.</p><br> 
        <form> 
         <input type="radio" name="stockOp" id="stockOption1" value="ship" /> 
         <label for="stockOption1">Ship what is available now</label><br> 

         <input type="radio" name="stockOp" id="stockOption2" value="hold" /> 
         <label for="stockOption2">Wait and ship together</label> 
        </form> 
       </td> 
      </tr> 
     <?php 
    } 
} 
add_action('woocommerce_cart_totals_after_order_total', 'notes_in_cart'); 
add_action('woocommerce_review_order_after_order_total', 'notes_in_cart'); 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

該代碼測試和工程。

+0

OP希望允許對產品進行延期交貨。如果您將產品設置爲「缺貨」,系統將不允許添加到購物車:https:// github。com/woocommerce/woocommerce/issues/11187 因此它必須與庫存數量而不是可用性狀態 –

+0

一起使用,這裏是另一個證明,如果is_in_stock()返回「outofstock」,則用戶無法檢出:) https:/ /github.com/woocommerce/woocommerce/issues/10834 –

+0

您還應該對產品上的backorders_allowed()進行檢查,以確保它們沒有數量0並且不允許在缺貨。當你在答案中更新時,我會提高你的評論的實用性。到目前爲止,如果產品的數量爲0或更少,即使沒有允許延期交貨,但仍然會顯示通知。 –

相關問題