简体   繁体   English

当来自特定产品类别的商品在 Woocommerce 的购物车中时禁用购物

[英]Disable shopping when an item from a specific product category is in cart in Woocommerce

I am trying to Disable shopping if a an item from a specific product category is in cart (which is a subscription in form of product with tabs - checkout and shipping are stripped off).如果来自特定产品类别的商品在购物车中(这是带有标签的产品形式的订阅 - 结帐和运输被剥离),我正在尝试禁用购物。 When that product is added to cart, no other products should be allowed to be add up.当该产品添加到购物车时,不允许添加其他产品。

I have tried those threads code:我试过这些线程代码:

But didn't helped.但没有帮助。

How can I disable shopping if a specific product category is in cart on Woocommerce?如果特定产品类别在 Woocommerce 的购物车中,如何禁用购物?

输出问题图像

October 2018 - Improved updated code version: 2018 年 10 月 - 改进的更新代码版本:
Disable other product categories for a cart item from specific category in Woocommerce 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别

Try the following code, that will:尝试以下代码,它将:

  1. Avoid add to cart when a product from a specific product category is in cart当来自特定产品类别的产品在购物车中时,避免添加到购物车
  2. Remove other cart items when a product from a specific product category is added to cart将特定产品类别中的产品添加到购物车时删除其他购物车项目

The code:编码:

// Remove other items when our specific product is added to cart
add_action( 'woocommerce_add_to_cart', 'remove_other_products_on_add_to_cart', 10, 6 );
function remove_other_products_on_add_to_cart ( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    // HERE set your product category (can be term IDs, slugs or names)
    $category = 'posters';

    // We remove other items when our specific product is added to cart
    if( has_term( $category, 'product_cat', $product_id ) ) {
        foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
            if( ! has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
                WC()->cart->remove_cart_item( $item_key );
            }
        }
    }
}

// Avoid other items to be added to cart when our specific product is in cart
add_filter( 'woocommerce_add_to_cart_validation', 'check_and_limit_cart_items', 10, 3 );
function check_and_limit_cart_items ( $passed, $product_id, $quantity ){
    // HERE set your product category (can be term IDs, slugs or names)
    $category = 'posters';

    // We exit if the cart is empty
    if( WC()->cart->is_empty() )
        return $passed;

    // CHECK CART ITEMS: search for items from product category
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
            // Display an warning message
            wc_add_notice( __('A subscription is already in cart (Other items are not allowed in cart).', 'woocommerce' ), 'error' );
            // Avoid add to cart
            return false;
        }
    }
    return $passed;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

暂无
暂无

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

相关问题 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce Woocommerce中特定产品类别的空购物车项目价格 - Null cart item price for a specific product category in Woocommerce 仅保留购物车中特定产品类别中最后添加的项目 WooCommerce - Keep in cart only last added item from specific product category in WooCommerce 在 Woocommerce 购物车结账和订单中禁用特定产品的项目名称链接 - Disable item name link for specific product in Woocommerce cart checkout and orders 从 WooCommerce 产品类别的购物车中隐藏“删除项目” - Hide “remove item” from cart for WooCommerce product category 为特定WooCommerce产品类别的购物车项目设置最小数量 - Set a minimum quantity for cart items from specific WooCommerce product category 在 WooCommerce 中为特定产品隐藏购物车中的“删除项目” - Hide “remove item” from cart for a specific product in WooCommerce 如果购物车包含特定的 Woocommerce 产品类别,则阻止添加到购物车 - Prevent add to cart if cart contains a specific Woocommerce product category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM