Offering a Buy 2 Get 1 Free (BOGO) promotion in your WooCommerce store is an excellent way to boost sales and increase customer engagement. While WooCommerce doesn’t have a built-in feature for this, you can achieve it using custom PHP code.
Experience lightning-fast speed, rock-solid security, and world-class support tailored for WordPress. Simply better hosting.
Why WooCommerce Doesn’t Offer BOGO
WooCommerce has a simple discount and coupon system, but it lacks advanced promotional features like BOGO. To create such discounts, you need to write custom PHP code for full control.
Suggested Read: Track and Display ‘Add to Cart’ Button Click Counts in WooCommerce
Add Buy 2 Get 1 Free Offer Using Custom Code
Since WooCommerce doesn’t provide a built-in BOGO feature, you can add the following PHP code to your theme’s functions.php
file. This method ensures that:
- The discount applies only when a total of 3 or more items (including quantities) are in the cart.
- The cheapest product is free.
Also Read: How to Add Extra Fees in WooCommerce (Without a Plugin)
Custom PHP Code for Buy 2 Get 1 Free (BOGO) Offer in WooCommerce
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
add_action('woocommerce_cart_calculate_fees', 'twapply_buy_2_get_1_discount'); function twapply_buy_2_get_1_discount() { if (is_admin() && !defined('DOING_AJAX')) { return; } $cart = WC()->cart; $cart_items = $cart->get_cart(); $total_quantity = 0; $prices = []; // Loop through cart items to count total quantity and store prices foreach ($cart_items as $cart_item_key => $cart_item) { $quantity = $cart_item['quantity']; $price = $cart_item['data']->get_price(); // Add each product's quantity to the total $total_quantity += $quantity; // Store price as many times as quantity for ($i = 0; $i < $quantity; $i++) { $prices[] = $price; } } // Apply discount only if there are at least 3 items in the cart if ($total_quantity >= 3) { asort($prices); // Sort prices in ascending order $lowest_price = reset($prices); // Get the lowest price // Apply discount if ($lowest_price > 0) { $cart->add_fee(__('Buy 2 Get 1 Free Discount', 'woocommerce'), -$lowest_price, true); } } } |
How This Works:
- ✅ Counts total product quantity instead of just product count.
- ✅ Sorts all items by price (considering quantities) and picks the lowest-priced one.
- ✅ Applies a discount equal to the cheapest product’s price if at least 3 total items are in the cart.
Are you in need of a skilled WordPress developer to bring your website vision to life?
Look no further! Whether you need custom themes, plugin development, site optimization, or ongoing support, I offer expert WordPress development services to suit your needs.
Advanced Customization Options
Apply the Discount Only for Specific Categories
If you want the BOGO offer to apply only to specific product categories, modify the code like this:
2 3 4 5 6 7 8 |
$allowed_categories = ['t-shirts', 'electronics']; // Replace with your category slugs $product_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', ['fields' => 'slugs']); if (!array_intersect($allowed_categories, $product_cats)) { continue; } |
Apply the Offer for Buy 1 Get 1 Free Offer
If you want to apply the offer Buy 1 Get 1 Free, replace this condition:
2 3 4 |
if ($total_quantity >= 2) { |
Exclude specific products from the BOGO offer
You can add an exclusion list inside the loop:
2 3 4 5 6 7 |
$excluded_products = [123, 456]; // Replace with product IDs if (in_array($cart_item['product_id'], $excluded_products)) { continue; } |
Apply the offer only for first-time buyers
You can check if the user has previous orders before applying the discount:
2 3 4 5 6 |
if (wc_get_customer_order_count(get_current_user_id()) > 0) { return; // Do not apply discount for returning customers } |
Are you want to get implementation help, or modify or extend the functionality?
A Tutorialswebsite Expert can do it for you.
Conclusion
A Buy 2 Get 1 Free offer is a great way to increase sales and customer satisfaction. You can implement it using custom PHP code for full control and flexibility.
By following this guide, you can easily integrate BOGO promotions into your WooCommerce store and customize them to suit your business needs.
Thanks for reading 🙏, I hope you found Track Add to Cart Button Click Counts in WooCommerce tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.
FAQs
No, WooCommerce does not have a built-in feature for BOGO (Buy One Get One Free) promotions. However, you can achieve this using custom PHP code or third-party plugins.
Yes, the code works with both simple and variable products.
Yes! You can modify the code to restrict the discount to specific categories.
No, the discount is applied at checkout. If you want to show a message on the product page or cart, you can use:
add_action(‘woocommerce_before_cart’, function() {
echo ‘
Buy 2 Get 1 Free offer is applied at checkout!
‘;
});
Yes, but you need to test compatibility with other discounts. Some coupons may override this discount, so ensure they don’t conflict.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co