Woocommerce 中基于购物车商品库存数量的自定义通知

Custom notice based on cart item stock quantity in Woocommerce

我正在尝试为 wordpress/woocommerce 创建一个函数,以便在购物车页面中显示一个用于拆分交付的选项。

它应该做的是检查购物车中所有商品的库存状态。

第一种情况:如果所有购物车中的商品都可用不输出任何内容。

第二种情况:如果所有购物车中的商品都缺货,什么都不输出。

第三种情况: 应该显示某些内容的唯一条件是,当两种情况(第一种和第二种)都发生时。

因此,只有当购物车有库存商品 AND 有商品缺货时,它才应该显示通知。

The previous version seemed to be the wrong way to go. So here is my new approach with a different code.

在functions.php中:

add_action( 'woocommerce_after_cart_table', 'split_devliery_notification' );        
function split_devliery_notification() {
    $all_items_in_stock = true; // initializing
    $all_items_out_of_stock = true;

    // Iterating through cart items (to get the stock info)
    foreach (WC()->cart->get_cart() as $cart_item) {

        # HANDLING SIMPLE AND VARIABLE PRODUCTS

        // Variable products
        $variation_id = $cart_item['variation_id'];
        if( 0 != $variation_id) {
            $variation_obj = new WC_Product_variation($variation_id);
            $stock = $variation_obj->get_stock_quantity();
        } else {
            // Simple products
            $product_id = $cart_item['product_id'];
            $product_obj = new WC_Product($product_id);
            $stock = $product_obj->get_stock_quantity();
        }

        if( $stock <= 0 ){
            // if an item is out of stock
            $all_items_in_stock = false;
            break; // We break the loop
        }
        elseif ( $stock >= 0 ) {
            $all_items_out_of_stock = false;
            break;
        }

    }

    // All items "in stock"
    if( $all_items_in_stock ) {
        echo 'All items in cart are in stock';
    } 
    elseif ( $all_items_out_of_stock ) {
        echo 'All items in cart are out of stock';
    }
    else {
        echo 'Some items in cart are in stock and some are out of stock -> Show notification ON!';
    }

}

此函数适用于两种情况:

  1. 如果购物车中的所有商品 有货 它会回应正确的消息(购物车中的所有商品都有货 ).
  2. If All items in cart are out of stock 回显正确的信息(All items in cart are out of stock)。

但是,如果购物车包含库存商品 AND 商品缺货,它会回显第一条消息(购物车中的所有商品都有库存)。

我已经重新访问了您的代码。对于购物车商品,$cart_item['data']; 是需要的 WC_Product 对象,因此代码会更加紧凑。

当购物车商品同时有货和缺货时,下面的代码将显示自定义通知。
第一个函数是检查购物车项目的条件函数。
第二个函数使用第一个函数有条件地显示自定义通知。

// Conditional function: Checking cart items stock
function is_mixed_stock_items(){
    $enough_stock = $out_of_stock = false; // initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Get an instance of the WC_Product Object
        $product = $cart_item['data'];
        // Get stock quantity
        $stock_qty = $product->get_stock_quantity();
        // get cart item quantity
        $item_qty  = $cart_item['quantity'];

        if( ( $stock_qty - $item_qty ) >= 0 ){
            $enough_stock = true; // enough stock
        } else {
            $out_of_stock = true; // not enough stock
        }
    }
    // return true if stock is mixed and false if not
    return $enough_stock && $out_of_stock ? true : false;
}

// Display a custom notice
add_action( 'woocommerce_after_cart_table', 'display_delivery_notification' );
function display_delivery_notification() {
    if( is_mixed_stock_items() ){
        $message = __("Some items in cart are in stock and some others out of stock -> Show notification ON!");
        wc_print_notice( $message, 'notice');
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

You can use the conditional function that check cart items stock in different hooks if needed

相关:

虽然来自@LoicTheAztec 的解决方案工作完美并且编码非常清晰、优雅,但我也想出了自己的解决方案。我也会 post 在这里,只是为了展示解决问题的两种不同方法。

add_action( 'woocommerce_after_cart_contents', 'split_devliery_notification' );        
function split_devliery_notification() {
$all_items_in_stock = true; // initializing a true statement at beginning to be proved
$all_items_out_of_stock = true;

// Iterating through cart items (to get the stock info)
foreach (WC()->cart->get_cart() as $cart_item) {

    # HANDLING SIMPLE AND VARIABLE PRODUCTS

    // Variable products
    $variation_id = $cart_item['variation_id'];
    if( 0 != $variation_id) {
        $variation_obj = new WC_Product_variation($variation_id);
        $stock = $variation_obj->get_stock_quantity();
    } else {
        // Simple products
        $product_id = $cart_item['product_id'];
        $product_obj = new WC_Product($product_id);
        $stock = $product_obj->get_stock_quantity();
    }

    if ( $stock <= 0 ){
        // if an item is out of stock
        $all_items_in_stock = false;
        break; // We break the loop
    }
}    
            // Continue with iterating if first iterating was stopped because one item has stock status below 0
            foreach (WC()->cart->get_cart() as $cart_item) {

                # HANDLING SIMPLE AND VARIABLE PRODUCTS

                // Variable products
                $variation_id = $cart_item['variation_id'];
                if( 0 != $variation_id) {
                    $variation_obj = new WC_Product_variation($variation_id);
                    $stock = $variation_obj->get_stock_quantity();
                } else {
                    // Simple products
                    $product_id = $cart_item['product_id'];
                    $product_obj = new WC_Product($product_id);
                    $stock = $product_obj->get_stock_quantity();
                }
                // check if stock status is 0 or above. If not it is proven that there are both types in cart
                if ( $stock >= 0 ) {
                    $all_items_out_of_stock = false;
                    break;
                }  
            }

// All items "in stock"
if( $all_items_in_stock ) {
// commented out to prevent displaying a message in that case
//        echo 'All items in cart are in stock'; 
} 
elseif ( $all_items_out_of_stock ) {
// commented out to prevent displaying a message in that case
//        echo 'All items in cart are out of stock';
}
else {
    ?>
        <div class="split-delivery-notification-container">
            <div class="split-delivery-notification-text">
                Delivery notification ON.
            </div>
        </div>
    <?php
     }

}

这进入 functions.php