Only Allow 1 Product in the Cart WooCommerce

Only Allow 1 Product in the Cart WooCommerce
527 Views
4.1
(8063)

Limiting sales of a product to just one per customer can be a smart move, especially if your items are special or you don’t have a lot of them in stock. This helps keep your stock under control and ensures more people can buy your product.

Why Sell Only One Item Per Customer?

Selling just one item per person can really help your business and keep your customers happy. here’s why:

  1. Makes products special: If each person can only buy one, it makes the item feel rare and important. This may make people consider it more valuable.
  2. Encourages Quick Purchases: When something is in short supply, people want to purchase quickly so they don’t miss out. This means they spend less time thinking and more time shopping.
  3. Increases value: If something is hard to find, people often want it more. They may also be willing to pay extra for it because it is considered better or more desirable.
  4. Keeps stock under control: For stores that don’t have a lot of inventory, this method helps ensure that you don’t sell out too quickly. This allows more customers to purchase.
  5. Makes customers loyal: When customers know they can get something unique from you, they come back. They appreciate that you offer something special.
  6. Prevents price wars: When your product is unique, people care more about getting it than getting the cheapest price. They’re buying because it’s something special, not because it’s the cheapest.

By limiting how many items a person can purchase, you make your products feel more special and ensure more customers get the chance to enjoy them.

/**
 * @snippet       WooCommerce Max 1 Product @ Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WCooCommerce 8
 * @community     https://businessbloomer.com/club/
 */
  
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 9999 );
   
function bbloomer_only_one_in_cart( $passed ) {
   wc_empty_cart();
   return $passed;
}

I am selling a product called “Product X”, which is available in two options: Variation A and Variation B.

I want to make sure that if a customer adds Variation A to their shopping cart, they can’t also add Variation B. The goal is to allow customers to choose only one option for Product X at a time.

However, I ran into a problem with the way I set it up. products in cart

Here is the code I am using:

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 );
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;
}

This code is supposed to prevent the customer from adding both variations of the Product.

Summary:

Setting a limit of one item per customer in your store can make products seem more special. This can have many benefits, such as being able to charge more for specific items and making customers feel an urgent need to purchase them immediately, often referred to as fear of missing out (FOMO).

You can easily set this using a free plugin called quantity-based pricing WooCommerce plugin. Just follow the simple steps given in the article above. If you are good at programming, you can also change the website code yourself, but this is generally not recommended unless you are a professional developer. It doesn’t cost anything to use the plugin, but you can pay to get more features if you find it useful.

How useful was this blog?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 8063

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *