【问题标题】:Custom notice based on cart item stock quantity in Woocommerce基于 Woocommerce 中购物车商品库存数量的自定义通知
【发布时间】:2019-02-16 05:21:18
【问题描述】:

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

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

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

第二种情况:如果购物车中的所有商品都缺货,则不输出任何内容。

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

因此,只有当购物车有库存商品并且有商品缺货时,它才会显示通知。

以前的版本似乎是错误的方式。所以这是我使用不同代码的新方法。

在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. 如果购物车中的所有商品缺货,则响应正确的消息(购物车中的所有商品都缺货)。

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

【问题讨论】:

  • 也许你应该 ping @cornel.raiu 看看他是否会为你这样做

标签: php wordpress woocommerce cart stock


【解决方案1】:

尽管来自@LoicTheAztec 的解决方案完美运行并且编码非常清晰、优雅且优雅,但我也提出了自己的解决方案。我也会在这里发布,只是为了展示解决问题的两种不同方法。

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中

【讨论】:

    【解决方案2】:

    我重新访问了您的代码。对于购物车项目,$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 文件中。经过测试并且可以工作。

    如果需要,您可以使用条件函数检查不同挂钩中的购物车物品库存

    相关:Get in WooCommerce cart the product ID of a cart item

    【讨论】:

    • 非常感谢@LoicTheAztec!在晚上,我也为我找到了一个可行的解决方案。但是您的解决方案更加优雅和美观!我喜欢你评论代码的方式。每次我从你那里看到一个解决方案,我都会学到很多东西。如果可以的话,我也可以发布我的解决方案。
    猜你喜欢
    • 2019-04-23
    • 2018-01-30
    • 2019-01-31
    • 1970-01-01
    • 2019-12-21
    • 2016-03-12
    • 2017-06-05
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多