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

问题描述 投票:1回答:2

如果购物车中有缺货商品,我需要在结帐订单中查看一个部分(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一起循环以确定是否缺货,但不确定如何。

php wordpress woocommerce checkout cart
2个回答
1
投票

您可以这样做来检查“库存数量”和“允许延期交货?”产品属性:

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()方法来完成,这些方法返回“库存状态” - “instock”或“outofstock”的值 - 但这不允许使用延期交货结账: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 );
}

这意味着它检查“库存状态”,如果“库存”则返回true,如果“缺货”则返回false。

现在,如果你在这里阅读:https://conschneider.de/manage-stock-backorders-woocommerce/

你会发现:

只要库存状态为“库存”,就可以使用延期交货。将产品设置为缺货将再次阻止购买并显示“缺货”。


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文件中,或者放在任何插件文件中。

此代码经过测试和运行。

© www.soinside.com 2019 - 2024. All rights reserved.