WordPress WooCommerce

WooCommerce Allow Only One Product In Cart

19 October 2021

Controlling the number of products a customer can add to their cart can be crucial in certain situations, such as when you want to limit purchases to a single item. In this article, we will show you how to allow only one product in the cart when using WooCommerce, a popular e-commerce plugin for WordPress.

The Code Snippet

Before we dive into the details, let’s take a look at the provided code snippet:

<?php
/**
* @snippet WooCommerce - allow only one product in cart
* @author Prosvit Design Team
* @website https://prosvit.design
*/
add_filter( 'woocommerce_add_to_cart_validation', 'prosvit_woocommerce_add_to_cart_validation', 99, 2 );
function prosvit_woocommerce_add_to_cart_validation( $passed, $added_product_id ) {
// Products ids that will trigger this behaviour
$products = array(1, 2, 3);
if ( in_array( $added_product_id, $products ) ) {
wc_empty_cart();
}
return $passed;
}

How the Code Works

The provided code snippet hooks into WooCommerce’s woocommerce_add_to_cart_validation filter. This filter is triggered when a product is added to the cart, allowing you to modify the validation process.

Here’s what each part of the code does:

  1. add_filter('woocommerce_add_to_cart_validation', 'prosvit_woocommerce_add_to_cart_validation', 99, 2);: This line adds a filter that hooks into the cart validation process. It specifies that the prosvit_woocommerce_add_to_cart_validation function should be executed when a product is added to the cart. The 99 priority ensures that this filter is applied late in the validation process, and 2 indicates that the function accepts two parameters.
  2. function prosvit_woocommerce_add_to_cart_validation( $passed, $added_product_id ) {: This defines the prosvit_woocommerce_add_to_cart_validation function, which will be called when a product is added to the cart. It accepts two parameters: $passed, which indicates if the product has passed validation, and $added_product_id, which is the ID of the product being added.
  3. $products = array(1, 2, 3);: This line defines an array of product IDs for which you want to enforce the restriction of having only one product in the cart. You can modify this array to include the product IDs you want to restrict.
  4. if ( in_array( $added_product_id, $products ) ) { wc_empty_cart(); }: This condition checks if the product being added has an ID that matches any of the product IDs in the $products array. If there is a match, it empties the cart using wc_empty_cart(), which removes all items from the cart.
  5. return $passed;: Finally, the function returns the value of $passed, which determines if the product is allowed to be added to the cart. Returning $passed ensures that the product’s validation status remains unchanged for products not affected by this restriction.

Implementing the Code

To implement this code on your WooCommerce store, follow these steps:

  1. Access Theme Functions: You can add this code snippet to your WordPress theme’s functions.php file. To do this, go to your WordPress dashboard, navigate to “Appearance” > “Theme Editor,” and find the functions.php file.
  2. Insert the Code: Paste the provided code snippet at the end of the functions.php file.
  3. Save Changes: Click the “Update File” button to save your changes.
  4. Modify the Product IDs: In the code snippet, you can customize the $products array to include the product IDs for which you want to enforce the one-product limit.

Once you’ve added and customized the code, your WooCommerce store will allow only one product from the specified list to be in the cart at a time. If a customer attempts to add another product from the restricted list, their cart will be emptied, ensuring that only one such product remains in the cart.

This restriction can be useful for various scenarios, such as limiting special offers to one per customer or enforcing exclusivity on certain products.

Does this snippet (still) work?

Please let us know in the comments if everything worked as expected. We would be happy to revise the snippet if you report otherwise (please provide screenshots).

Leave a Reply

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

Array

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert