dourang20110122 2018-08-20 21:30
浏览 85
已采纳

当特定产品类别的商品在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?

output problem image

  • 写回答

1条回答 默认 最新

  • doucuyu2259 2018-08-21 05:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示