Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Automatically Add Free Product to Cart Based on Product ID in WooCommerce?

Are you looking for a seamless way to surprise your customers with WooCommerce BOGO “Buy One Get One” offers ? In this guide, we’ll show you how to implement it when customers purchase specific products on your WooCommerce store. This approach lets you surprise your customers with special deals only on selected items, increasing the effectiveness of your promotions. Let’s see how to boost sales with such targeted BOGO promotions.

Solution: Create BOGO (Buy One Get One) Offers Based on Product IDs in WooCommerce

The provided code snippet dynamically adds a gift based on the presence of a particular product as specified in the code product_id=34).

add_action( 'template_redirect', 'ts_add_gift_id_in_cart' );
 
function ts_add_gift_id_in_cart() {
 
   if ( is_admin() ) return;
   if ( WC()->cart->is_empty() ) return;
 
   $product_bought_id = 34;
   $product_gifted_id = 25;
 
   // see if product id in cart
   $product_bought_cart_id = WC()->cart->generate_cart_id( $product_bought_id );
   $product_bought_in_cart = WC()->cart->find_product_in_cart( $product_bought_cart_id );
 
   // see if gift id in cart
   $product_gifted_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
   $product_gifted_in_cart = WC()->cart->find_product_in_cart( $product_gifted_cart_id );
 
   // if not in cart remove gift, else add gift
   if ( ! $product_bought_in_cart ) {
      if ( $product_gifted_in_cart ) WC()->cart->remove_cart_item( $product_gifted_in_cart );
   } else {
      if ( ! $product_gifted_in_cart ) WC()->cart->add_to_cart( $product_gifted_id );
   }
}
flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

When a customer adds the T-shirt with a logo to their cart, another product (a Polo) will automatically be added to their cart for free during the checkout process. If the customer removes the T-shirt with a logo from their cart, the free Polo product will also be removed from the cart page.

Add Free Product to Cart Based on Product ID

Similarly, you can also automatically add a product to your WooCommerce cart in 6 different scenarios such as based on cart total, product tags, excluding  Buy One Get One offers for expensive items, product categories, etc.

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

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x