Search the site:

How to automatically add a product to your WooCommerce cart in 3 different scenarios

We all love surprises. Isn’t it? 🙂 Surprise can be anything like a birthday surprise or maybe receiving a gift unexpectedly. You as a Store owner, running a store on the WooCommerce platform, have you ever thought to surprise your customers with some discounted or FREE stuff? Hell yeah!

In this post, we will see how simple it is to automatically add a product to your WooCommerce cart as per 3 different scenarios listed below:

1. Add to Cart action: When Product A is added to cart, then also add Product B to cart
2. Condition: When the cart total reaches certain amount
3. Website Visit: Adding product to the cart when a customer visits your website

This post will be knowledgeable to store owners as well as developers. The store owner can simply place the code snippets in functions.php and the developer can see the logic for automatically adding the product to cart.

1. Add to Cart action: When Product A is added to cart, then also add Product B to cart

Suppose you are selling Sports & Fitness Equipments on your store and you want to gift Bat Grip when the customer adds the Cricket Bat in their basket. Let’s see how we can achieve this.

First, we will create the product called MRF Cricket Bat and we will add it to cricket bat product category. The Bat Grip will be the free product in your store.

The customer is on the MRF Cricket Bat page and adding it to the cart so the Bat Grip should automatically get added to the cart.

Automatically adding product to cart on Add to Cart button click
Automatically adding product to cart on Add to Cart button click

To achieve this, WooCommerce provides woocommerce_add_to_cart action hook which will execute on Add to Cart button click. Using the same we can create our custom code as shown below.

As you can see in the above code snippet, we have used the woocommerce_add_to_cart action hook and it will execute the code inside the aaptc_add_product_to_cart() callback function. We will get the product ID from function arguments and based on the product ID we are fetching the categories. You can get the ids of the categories for the product using wc_get_product_term_ids().

Next, we are checking if the product which is being added to the cart is coming under cricket bat category and the Bat Grip product is not present in the cart then adds it.

2. Condition: When the cart total reaches certain amount

Suppose you are selling Electronic items on your store and you want to gift cleaning kit if the cart total is more than the particular amount.

To achieve this, we can use the same code snippet used for automatically adding the product to the basket when the customer visits the website.

Just the difference will be that we will use template_redirect action hook instead of init hook to add some more conditions in the code snippet.

The init hook is executing before the Cart Calculations performed by WooCommerce so no cart information will be available. Hence we are using the template_redirect hook to get the cart total.

As you can see in the below code snippet, I have used $woocommerce->cart->total to get the cart total. And compared it with the total amount required to automatically adding Cleaning Kit to the cart.

Automatically adding product to cart when cart total more than $500
Automatically adding product to cart when cart total more than $500

3. Website visit: Adding product to the cart when a customer visits your website

 

Let’s say you are selling Sports & Fitness Equipments on your store and you want that when a customer is visiting your website then a Cap should automatically get added to their basket as a gift.
Automatically adding product to cart on website visit
Automatically adding product to cart on website visit

WordPress has init hook using which we can implement our custom code snippet and place it in functions.php file to automatically add the product to your WooCommerce cart on website visit. Below is the code snippet to achieve the same.

Let’s understand what I have done in the above code snippet. As you can in the above code snippet we used init WordPress hook and it will call the aaptc_add_product_to_cart() function. In this function, we are fetching the cart items using $woocommerce->cart->get_cart() and checking if the free product is present in the cart or not. If not then add it to the cart using WC()->cart->add_to_cart() function else don’t do anything.

We can also use template_redirect action hook.

Modifying price of the automatically added product to the cart:

Suppose your free product’s Regular Price is $25 and if that product is automatically get added to your cart when cart total is reached to a particular amount then you want to modify that freely added product’s price to $0. For that, you can use the below code snippet to modify the free product’s price in the cart.

Why this topic?

The idea of writing this post came from one of our clients using Booking & Appointment plugin for WooCommerce for Tour Business. His requirement was, when the customer is booking the tour then automatically add the company cap as a free product to their basket.

Adding Cap to basket when booking the tour

As you can see in the above gif, I have added booking for New York City All Around Town Hop-on Hop-off Tour by selecting the start date as 9th November 2017. Adding this booking to the cart is automatically adding the Cap product to the cart.

Conclusion

In this post, we have seen how easy it is to automatically add a product to your WooCommerce cart on website visit, on Add to Cart button click and based on the total of the cart. There might be many other scenarios to automatically adding the free product to your customer’s basket.

Feel free to mention your questions in the comments below and we shall get back to you as early as possible. If you are interested in the custom work then you send your requirements on the below email address.
Email: kartik at tychesoftwares dot com

Browse more in: WooCommerce Tutorials, Code Snippets, WooCommerce How Tos
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

104 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Stef
Stef
3 years ago

The first snippet didn’t work. Has something changed since 4.3.1? I get an error stating the item could not be added to the cart. It would be nicer to use Product rather than Category too.

Stef
Stef
3 years ago
Reply to  Stef

Never mind I got it to work now.

Stef
Stef
3 years ago

Hey guys, can you do an array? I’ve been trying to add a second snippet to another product but it crashes the site.

Mark
Mark
3 years ago

This is great but if the user deletes the main paid product the free product still stays in the cart and the user can get away with only paying for shipping for that. That’s not good.

Stef Serafin
Stef Serafin
3 years ago
Reply to  Kartik Parmar

I get that but you sorta have a responsibility to showcase how to remove the Free or Additional product when Parent is bought because a lot of owners could lose lots of money. Like if I was going to show people how to do this I’d also add how to write a condition that removes the gift from the cart when the user deletes the parent product. That’s just standard good coding practice.

Stef
Stef
3 years ago
Reply to  Kartik Parmar

Awesome! Sorry for delay. Been moving

Control Alt Delete
Control Alt Delete
2 years ago
Reply to  Kartik Parmar

Hi Kartik – First of all, thank you for this code snippet on removing the free product when the parent is removed from cart. However, when this code (https://gist.github.com/kartikparmar/d8e0ee06b5dcba0eab4dc4d87ec66405) is added to my site’s functions.php it does not work. In fact, nothing happens. Can you confirm if the code for removing the free product when the parent is removed from cart works with WP 5.x? The only variables that I changed are: $prod_to_remove = xxxx; // Product ID of Free Product $parent_product_id = xxxx; // Product ID of Parent Product Also – The code in #1 has a free “product”… Read more »

Control Alt Delete
Control Alt Delete
2 years ago
Reply to  Kartik Parmar

Hello again,

Thank you for the response. And yes, your understanding of my second point is correct:
“the free product should get removed when cart doesn’t contain any product of a particular category?”

Are you able to provide a github url with the code snippet containing this variation?

Cheers!

Control Alt Delete
Control Alt Delete
2 years ago
Reply to  Kartik Parmar

Hi,

Unfortunately, the code in this link is not producing the desired effect, and did not remove the free product as we hoped. (Link: https://gist.github.com/kartikparmar/fc39177351aed9b2275e82b037e40663). Any other ideas?

Dian
3 years ago

how if i use the sub catgory insted of parent category?

Mark
Mark
3 years ago
Reply to  Dian

You can use whatever category you want. Just reference the cat ID number

Elena
Elena
3 years ago

The first snippet is crushing the website:
HTTP ERROR 500
I changed only $product_id and $free_product_id
I am using WordPress Version 4.9.1
Is there anything else I need to change on the code?

Elena
Elena
3 years ago
Reply to  Kartik Parmar

Thank you very much, Kartik! I appreciate your help!

from purchased

Subscribe for more offers

Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins and 50% OFF on Bundles
104
0
Would love your thoughts, please comment.x
()
x