Running a Buy 2 Get 1 Free (BOGO) promotion is an excellent strategy to boost sales and encourage customers to purchase more products. While WooCommerce does not have a built-in feature for this type of offer, you can achieve it using custom PHP code.
Suggested Read: Partial COD (Cash on Delivery) in WooCommerce
Why Add a Buy 2 Get 1 Free [BOGO] Offer?
BOGO deals can:
- ✅ Increase average order value
- ✅ Encourage customers to buy more items
- ✅ Help clear old stock
- ✅ Enhance customer satisfaction
As you know that, WooCommerce does not provide a built-in method to apply BOGO offers. So, we will use custom PHP code to implement it.
Recommended: How to Add Extra Fees in WooCommerce (Without a Plugin)
Step 1: Understanding the BOGO Offer Mechanism
Our approach will:
- Check if the cart contains at least 3 products (quantity-based, not just different items).
- Find the cheapest product in the cart.
- Apply a discount equal to the price of the lowest item.
- Ensure the discount is visible at checkout.
Also Read: Check Pincode / Zipcode Code for WooCommerce Shipping Availability
Step 2: Add Custom PHP Code in functions.php
To apply the Buy 2 Get 1 Free discount, add the following PHP code to your theme’s functions.php
file:
add_action('woocommerce_cart_calculate_fees', 'woobooster_apply_buy_2_get_1_free_discount');
function woobooster_apply_buy_2_get_1_free_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();
$total_quantity += $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
if ($lowest_price > 0) {
$cart->add_fee(__('Buy 2 Get 1 Free Discount', 'woocommerce'), -$lowest_price, true);
}
}
}
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.
How This Code Works
- Counts total quantity of products in the cart (not just unique products).
- Finds the cheapest product in the cart.
- Applies a discount equal to the cheapest product’s price if 3 or more products are added.
- Automatically adjusts based on different product prices.

Step 3: Displaying a Message in the Cart
Since this discount is applied at checkout, you might want to inform customers before they complete their purchase. Add this snippet to show a message in the cart:
add_action('woocommerce_before_cart', function() {
echo '<p style="color: green; font-weight: bold;">Buy 2 Get 1 Free offer applied at checkout!</p>';
});
Step 4: Exclude Certain Products from the Offer
If you want to exclude specific products, modify the loop like this:
$excluded_products = [123, 456]; // Replace with product IDs
if (in_array($cart_item['product_id'], $excluded_products)) {
continue;
}
Conclusion
Adding a Buy 2 Get 1 Free offer in WooCommerce using custom code gives you full control over the discount structure without relying on plugins.
Thanks for reading 🙏, I hope you will get this code snippet helpful for your project.
This method helps you increase sales while keeping customers happy! 🎉
Would you like help customizing it further? Contact Us