检查购物车中是否有缺货商品 - WooCommerce

Check if there are out of stock items in cart - WooCommerce

仅当购物车中有缺货商品(b/c 我们的产品可延期交货时,我才需要在我的结账订单评论中显示一个部分。这是显示部分的代码...

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 循环来确定商品是否缺货,但不确定如何处理。

您可以这样做来检查产品的 "Stock quantity" 和 "Allow Backorders?" 属性:

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() 方法完成 returns "Stock status" - 'instock' 或 'outofstock' - 但不允许使用延期交货结帐:https://github.com/woocommerce/woocommerce/issues/11187 or https://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 );
}

这意味着它检查 "Stock Status",如果 "in stock" 则 returns 为真,如果 "out of stock" 则为假。

现在,如果您阅读此处:https://conschneider.de/manage-stock-backorders-woocommerce/

你会发现:

Backorders only are possible for as long as the stock status is “in stock”. Setting the product to out of stock will again block purchases and display “out of stock”.

更新: 由于您的所有产品都可以延期交货,唯一有效的方法是登记 foreach循环 如果每个购物车商品数量的库存数量 "is sufficient"...

If the stock quantity of the product is smaller than the cart item quantity (WC displays "available on backorder"), then your "shipment note" is displayed.

这是代码:

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

此代码已经过测试并且有效。