Example Cart Restrictions in WooCommerce

I’ve been working with a “Trial Product” in a WooCommerce store which needs to be the only item in the cart during checkout due to shipping requirements (and because it doesn’t make sense to order a trial if you’re also going to order the actual product).

To make this clear to the customer, I’ve restricted what can be added to the cart in specific situations:

1) If the “Trial Product” is already in the cart, additional products should not be added. WooCommerce will instead display a notice asking the customer to remove the “Trial Product” from their cart if they wish to add different products.

2) If products are already in the cart, and the customer attempts to add the “Trial Product”, a notice will display asking the customer to remove the other items from their cart first.

Here’s what the code looks like:

[gist id=”ff6fa702e308aa07d9cfcd206aac3006″ oembed=”1″]

6 Responses

  1. Thanks for this code snippet! I’m trying to do the same thing in a WooCommerce setup I’m working on. However, I have implemented the code above and you can still add products to the cart if the Trial Product is already in the cart. Any update to the code

      1. Yes, the Trial Product ID has been updated. The code snippet you came up with works when there are other products in the cart, but when the Trial Product is already in the cart other products can still be added.

  2. Nice piece of code works beautifully. How would the code look like for adding multiple Id’s of products that can be ordered together but not with other products?

  3. This is pretty much exactly what I need, but it errors if I try to add the trial product more than once to the cart. I should be able to add more than Qty 1 of the trial product to the cart but can’t seem to figure out what needs to change in the code after trying for a couple hours.

  4. Hi I am not a programmer, please can you help. if the trial product in the cart is the trial product, how can the code be adjusted to make provision for variation products?

    I have found when the product in the cart is the trial product and not a variation product the code works and the correct message is displayed. however when the trial product in the cart is not a variation product the code allows none trial products to be added to the cart.

    Thanks, any assistance will be appreciated.

    Arthur

Leave a Reply to Devin Cancel reply