Search the site:

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

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 may be receiving a gift unexpectedly. You as a Store owner, running a store on 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 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 the 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 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 cart is coming under cricket bat category and the Bat Grip product is not present in the cart then add 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 customer visits 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 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 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 start date as 9th November 2017. Adding this booking to cart is automatically adding the Cap product to the cart.

Conclusion

In this post, we have seen that how easy it is to automatically add a product to 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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

63
Leave a Reply

avatar
21 Comment threads
42 Thread replies
20 Followers
 
Most reacted comment
Hottest comment thread
25 Comment authors
Adebola AdeolaRobAdam BJohannThomas Recent comment authors

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

  Subscribe  
newest oldest
Notify of
Stef
Guest
Stef

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
Guest
Stef

Never mind I got it to work now.

Stef
Guest
Stef

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
Guest
Mark

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.

Dian
Guest

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

Mark
Guest
Mark

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

Elena
Guest
Elena

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?

from purchased

Subscribe Now