基于Woocommerce中购物车项目库存数量的自定义通知 [英] Custom notice based on cart item stock quantity in Woocommerce

查看:120
本文介绍了基于Woocommerce中购物车项目库存数量的自定义通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a function for wordpress/woocommerce to show an option in the cart page for split delivery.

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

What it should do is to check the stock status of all items in cart.

首先情况:如果购物车中的所有项目均可用,则不输出任何内容。

First case: If all items in cart are available output nothing.

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

Second case: If all items in cart are out of stock, output nothing.

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

Third case: The only condition when something should be shown is, when both cases (first and second) occurs.

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

So only if the cart has items in stock AND has items out of stock it should display a notification.


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

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

在functions.php中:

in 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!';
    }

}

此功能适用于两种情况:

This function works for two cases:


  1. 如果购物车中的所有物品都在有货,它将回显正确的消息(购物车中的所有物品都在库存中
    )。

  2. 如果购物车中的所有物品缺货,则会显示正确的消息(购物车中的所有物品缺货)。 / li>
  1. If all items in cart are in stock it echoes the right message (All items in cart are in stock ).
  2. If all items in cart are out of stock is echoes the right message (All items in cart are out of stock).

但是,如果购物车中有库存的商品 AND 缺货,它将回显第一个消息(所有商品购物车中有现货)。

But if the cart contains items in stock AND items out of stock it echoes the first message (All items in cart are in stock).

推荐答案

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

I have revisited your code. For cart items, $cart_item['data']; is the WC_Product Object that is needed, so the code will be much more compact.

当同时有存货和缺货的购物车有混合存货时,以下代码将显示自定义通知。

第一个功能是检查购物车物品的条件功能。

第二个函数使用第一个函数有条件地显示自定义通知。

The code below will display a custom notice when there is mixed stock for cart items that are in stock and out of stock at the same time.
The first function is the conditional function that checks cart items.
The second function use the first function to display conditionally a custom notice.

// 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文件中主题(或活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


您可以使用条件函数来检查购物车中有不同挂钩的物品

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

相关:在WooCommerce购物车中获取购物车商品的产品ID

这篇关于基于Woocommerce中购物车项目库存数量的自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆