简体   繁体   English

如果购物车包含特定的 Woocommerce 产品类别,则阻止添加到购物车

[英]Prevent add to cart if cart contains a specific Woocommerce product category

My question and code is directly based off of this question - " I have a store containing 5 categories. It's multivendor and due to shipping complications, I can only sell products from one specific category ('paint') when they are alone in the cart. So I need to prevent add to cart of any non-paint products when the cart contains paint, and display an error message, and I need to prevent paint products being added to a cart containing any other categories, and disaply an error message.我的问题和代码直接基于这个问题 - “我有一家包含 5 个类别的商店。它是多供应商的,并且由于运输复杂性,我只能在购物车中单独销售一个特定类别('油漆')的产品. 所以我需要防止在购物车包含油漆时将任何非油漆产品添加到购物车,并显示错误消息,并且我需要防止将油漆产品添加到包含任何其他类别的购物车,并显示错误消息。

I've tried to cobble together this code from snippets I've found lying around on Stackoverflow and elsewhere.我试图从我在 Stackoverflow 和其他地方发现的代码片段中拼凑出这段代码。 The logic seems to work in my brain, but when I try to implement the code (through functions.php) it prevents any product from being added to the cart unless the cart is empty."这个逻辑似乎在我的大脑中起作用,但是当我尝试实现代码(通过functions.php)时,它会阻止任何产品被添加到购物车中,除非购物车是空的。”

Link to question - Prevent add to cart if cart contains a specific category (WooCommerce)链接到问题 - 如果购物车包含特定类别,则阻止添加到购物车 (WooCommerce)

I have checked and modified the accepted answer to suit my needs however it doesnt seem to work at all as i have the same question just related to a different category (ie gas instead of paint), it was my understanding that I would just have to modify the "has_term" function in order to get this to work as it is just a matter of pointing to a particular category?我已经检查并修改了接受的答案以满足我的需要,但是它似乎根本不起作用,因为我有相同的问题只是与不同的类别(即气体而不是油漆)有关,我的理解是我只需要修改“has_term” function 以使其正常工作,因为它只是指向特定类别的问题?

//*** Prevent mixture of gas and other prods in same cart ***//
function dont_add_gas_to_cart_containing_other($validation, $product_id) {

// Set flag false until we find a product in gas
    $cart_has_gas = false;

// Set $cat_check true if a cart item is in gas cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

        if (has_term('Gas', 'Gas Tanks & Accessories', $product->id)) {
            $cart_has_gas = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_gas = false;
    if (has_term('Gas', 'Gas Tanks & Accessories', $product_id)) {
        $product_is_gas = true;
    }

// Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains gas and product to be added is not gas, display error message and return false.
        if ($cart_has_gas && !$product_is_gas) {
            wc_add_notice('Sorry, you can only purchase Helium Gas products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
        // If cart contains a product that is not gas and product to be added is gas, display error message and return false.
        elseif (!$cart_has_gas && $product_is_gas) {
            wc_add_notice('Sorry, you can only purchase Helium Gas products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_gas_to_cart_containing_other', 10, 2);

Because you gave the category name to the function, you must give the function a slug, second parameter should be 'product_cat' and last parameter should be a Product ID .因为您给 function 提供了类别名称,所以您必须给 function 一个 slug,第二个参数应该是'product_cat'而最后一个参数应该是Product ID

$product = $cart_item['data'];

if( has_term( 'gas', 'product_cat', $product->get_id() ) ) {
    ......
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果购物车包含特定类别(WooCommerce),则阻止添加到购物车 - Prevent add to cart if cart contains a specific category (WooCommerce) 更改Woocommerce 3中特定产品类别的“添加到购物车”文本 - Change the add to cart text for a specific product category in Woocommerce 3 如何隐藏添加到购物车按钮特定角色和产品类别 Woocommerce - How to hide add to cart button specific role and product category Woocommerce "允许将 Woocommerce 中特定产品类别的最多 3 个产品添加到购物车" - Allow add to cart 3 products max for a specific product category in Woocommerce 在WooCommerce中为特定产品类别自定义“添加到购物车”按钮 - Customize “Add to cart” button for a specific product category in WooCommerce Ajax上的JS警报已添加到购物车,以获取Woocommerce中特定产品类别的数量 - JS alert on ajax add to cart for specific product category count in Woocommerce 在WooCommerce购物车项目上为特定产品类别添加正文类 - Add a body class for a specific product category on WooCommerce cart items 删除 Woocommerce 中特定产品类别的添加购物车按钮 - Remove add cart button in Woocommerce for a specific product category WooCommerce重定向到添加到购物车上的产品类别 - WooCommerce redirect to product category on add to cart 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM