Looking to spice up your WooCommerce store with enticing promotions? Let’s say your customers browsing through your online shop, stopped by a “Buy 2, Get 1 Free” offer on their favorite products.
Such BOGO promotions, especially when paierd with WooCommerce Bundled Products makes wonders. For instance, think of an “Electronic Bundle Deal” where buying a TV and a stabilizer together earns customers a high-quality wireless speaker for free. Such offers not only boost sales but also add excitement and value for your shoppers.
In this guide, we’ll show you how to effortlessly set up Buy 2 Get 1 offer programatically in your WooCommerce store.
Solution: Set Up a BOGO Deal: “Buy 2 and Get 1” Offer in WooCommerce
This code snippet sets up a functionality in WooCommerce where a specific gift product (product C) is added to the cart conditionally based on the presence of two other products (product A and product B)
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_a_id = 100; $product_b_id = 457; $product_gifted_id = 470; // see if product A in cart $product_a_cart_id = WC()->cart->generate_cart_id( $product_a_id ); $product_a_in_cart = WC()->cart->find_product_in_cart( $product_a_cart_id ); // see if product B in cart $product_b_cart_id = WC()->cart->generate_cart_id( $product_b_id ); $product_b_in_cart = WC()->cart->find_product_in_cart( $product_b_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 both products A and B are in cart, add gift C if ( $product_a_in_cart && $product_b_in_cart ) { if ( ! $product_gifted_in_cart ) { WC()->cart->add_to_cart( $product_gifted_id ); } } else { // if not both products A and B are in cart, remove gift C if present if ( $product_gifted_in_cart ) { WC()->cart->remove_cart_item( $product_gifted_in_cart ); } } }
Output
The free product gets automatically added to the cart, only when a customer purchases both products A and B. The free product doesn’t get added if only one of the specified products is added to the cart.
![How to Set Up a BOGO Deal: "Buy 2 and Get 1" Offer in WooCommerce? 1 How to Set Up a BOGO Deal: "Buy 2 and Get 1" Offer in WooCommerce? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/Buy-Product-A-B-and-get-Product-C-for-free-1024x363.png)
Enhance your promotional efforts and start exploring more BOGO promotions such as to implement buy 1 get 2 free action in WooCommerce which handles the bogo gift offering under various conditions.