Woocommerce中购物车中有特定产品类别的商品时,禁用购物 [英] Disable shopping when an item from a specific product category is in cart in Woocommerce

查看:92
本文介绍了Woocommerce中购物车中有特定产品类别的商品时,禁用购物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果购物车中有某个特定类别的商品,我正在尝试禁用购物功能(这是带有标签的产品形式的订阅-结帐和发货被剥离)。将该产品添加到购物车后,不得再添加其他产品。



我已经尝试过这些线程代码:




  • 解决方案


    2018年10月-改进的更新代码版本:
    禁用其他Woocommerce中特定类别的购物车产品的其他产品类别


    尝试以下代码,将:


    1. 当来自特定产品类别的产品在购物车中时,请避免添加到购物车

    2. 在以下情况下删除其他购物车项将来自特定产品类别的产品添加到购物车

    代码:

      //当我们将特定产品添加到购物车中时,删除其他项目
    add_action('woocommerce_add_to_cart','remove_other_products_on_add_to_cart',10,6);
    函数remove_other_products_on_add_to_cart($ cart_item_key,$ product_id,$ quantity,$ variation_id,$ variation,$ cart_item_data){
    //在此设置您的产品类别(可以是术语ID,段塞或名称)
    $ category ='海报';

    //我们将特定产品添加到购物车中时,我们会删除其他项目
    if(has_term($ category,'product_cat',$ product_id)){
    foreach(WC( )->购物车-> get_cart()as $ item_key => $ cart_item){
    if(!has_term($ category,'product_cat',$ cart_item ['product_id'])){
    WC()->购物车-> remove_cart_item($ item_key);
    }
    }
    }
    }

    //避免当我们的特定产品在购物车中时将其他项目添加到购物车
    add_filter ('woocommerce_add_to_cart_validation','check_and_limit_cart_items',10,3);
    function check_and_limit_cart_items($ passed,$ product_id,$ quantity){
    //在此设置您的产品类别(可以是术语ID,段塞或名称)
    $ category =海报;

    //如果购物车为空$$$$ if(WC()-> cart-> is_empty())
    return $ pass;

    //检查购物车项目:从产品类别
    foreach中搜索商品(WC()-> cart-> get_cart()as $ cart_item){
    if( has_term($ category,'product_cat',$ cart_item ['product_id'])){
    //显示警告消息
    wc_add_notice(__('订购已经在购物车中(不允许其他物品在购物车中), woocommerce),错误);
    //避免添加到购物车
    返回false;
    }
    }
    返回$ pass;
    }

    代码进入活动子主题(或活动主题)的function.php文件)。经过测试,可以正常工作。


    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?

    解决方案

    October 2018 - Improved updated code version:
    Disable other product categories for a cart item from specific category in 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). Tested and works.

    这篇关于Woocommerce中购物车中有特定产品类别的商品时,禁用购物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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