dourang20110122 2018-08-20 13: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-20 21: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:

    1. // Remove other items when our specific product is added to cart
    2. add_action( 'woocommerce_add_to_cart', 'remove_other_products_on_add_to_cart', 10, 6 );
    3. function remove_other_products_on_add_to_cart ( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    4. // HERE set your product category (can be term IDs, slugs or names)
    5. $category = 'posters';
    6. // We remove other items when our specific product is added to cart
    7. if( has_term( $category, 'product_cat', $product_id ) ) {
    8. foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
    9. if( ! has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
    10. WC()->cart->remove_cart_item( $item_key );
    11. }
    12. }
    13. }
    14. }
    15. // Avoid other items to be added to cart when our specific product is in cart
    16. add_filter( 'woocommerce_add_to_cart_validation', 'check_and_limit_cart_items', 10, 3 );
    17. function check_and_limit_cart_items ( $passed, $product_id, $quantity ){
    18. // HERE set your product category (can be term IDs, slugs or names)
    19. $category = 'posters';
    20. // We exit if the cart is empty
    21. if( WC()->cart->is_empty() )
    22. return $passed;
    23. // CHECK CART ITEMS: search for items from product category
    24. foreach ( WC()->cart->get_cart() as $cart_item ){
    25. if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
    26. // Display an warning message
    27. wc_add_notice( __('A subscription is already in cart (Other items are not allowed in cart).', 'woocommerce' ), 'error' );
    28. // Avoid add to cart
    29. return false;
    30. }
    31. }
    32. return $passed;
    33. }

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

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部