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

How to Create BOGO (Buy One Get One) Offers Based on Product IDs in WooCommerce?

Are you looking for a seamless way to surprise your customers with BOGO “Buy One Get One” offers when they purchase specific products on your WooCommerce store? If so, we’ve got just the solution for you! The provided code snippet checks the contents of a user’s shopping cart and dynamically adds or removes a gift based on the presence of a particular product.

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 );
   }
}

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.

BOGO Offers Based on Product IDs in WooCommerce

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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x