最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Allow only 1 quantity of particular product in cart WooCommerce

programmeradmin1浏览0评论

I have one variable product X that product has two variations, Variations A and Variations B.

If a customer added variation A in cart then I want to block the customer to add variations B in cart. At a time customer only order one of variations of that product.

I have added below code but it's not working well because if I added one variations of product X in cart then I tried to add another product Y in the cart it's not adding to cart.

My code current code is the following.

function wph_add_the_cart_validation_for_zoomarine_e_ticket( $passed ) { 
    // The product id of variable product X     
    $product_id = 44050;
    $in_cart = false;

    foreach( WC()->cart->get_cart() as $cart_item ) {
        $product_in_cart = $cart_item['product_id'];
       if ( $product_in_cart === $product_id ) $in_cart = true;
    }

   if ( $in_cart )  { ?>
       <script type="text/javascript">
           alert("The product is already in cart. You can only add one E ticket per order");
        </script>
   <?php
       $passed = false;
   }
   return $passed;
}

add_filter( 'woocommerce_add_to_cart_validation', 'wph_add_the_cart_validation_for_zoomarine_e_ticket', 10, 5 );

I have one variable product X that product has two variations, Variations A and Variations B.

If a customer added variation A in cart then I want to block the customer to add variations B in cart. At a time customer only order one of variations of that product.

I have added below code but it's not working well because if I added one variations of product X in cart then I tried to add another product Y in the cart it's not adding to cart.

My code current code is the following.

function wph_add_the_cart_validation_for_zoomarine_e_ticket( $passed ) { 
    // The product id of variable product X     
    $product_id = 44050;
    $in_cart = false;

    foreach( WC()->cart->get_cart() as $cart_item ) {
        $product_in_cart = $cart_item['product_id'];
       if ( $product_in_cart === $product_id ) $in_cart = true;
    }

   if ( $in_cart )  { ?>
       <script type="text/javascript">
           alert("The product is already in cart. You can only add one E ticket per order");
        </script>
   <?php
       $passed = false;
   }
   return $passed;
}

add_filter( 'woocommerce_add_to_cart_validation', 'wph_add_the_cart_validation_for_zoomarine_e_ticket', 10, 5 );
Share Improve this question asked Oct 7, 2019 at 10:23 developermedeveloperme 1415 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try with this

add_filter( 'woocommerce_add_to_cart_validation', 'allowed_products_variation_in_the_cart', 10, 5 );

function allowed_products_variation_in_the_cart( $passed, $product_id, $quantity, $variation_id, $variations) {

    $product_a = 14576;
    $product_b = 14091;

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $cart_product_id = $cart_item['product_id'];
        if ($cart_item['variation_id']) {
            $cart_product_id = $cart_item['variation_id'];
        }

        if ( ($cart_product_id == $product_a && $variation_id == $product_b) || ($cart_product_id == $product_b && $variation_id == $product_a) ) {

            wc_add_notice(__('You can\'t add this product.', 'domain'), 'error');
            $passed = false; // don't add the new product to the cart
            // We stop the loop
            break;
        }
    }

    return $passed;
}
发布评论

评论列表(0)

  1. 暂无评论