Buy X Get Y Free (BOGO) offers are one of the most effective promotional strategies in eCommerce.
They encourage customers to buy more products while feeling like they are getting extra value.
In this tutorial, you will learn how to create a simple “Buy X Get Y Free” system in WooCommerce using hooks.
Why BOGO Offers Increase Sales
These offers work because they combine urgency and value perception.
- Increases average order value
- Encourages bulk purchases
- Improves customer satisfaction
- Helps move slow-moving inventory
The WooCommerce Hook We Will Use
We will use:
woocommerce_before_calculate_totals
This hook allows us to modify cart contents before totals are calculated.
Example Code
Add this code to your theme’s functions.php file or a custom plugin:
add_action('woocommerce_before_calculate_totals', 'bogo_buy_x_get_y_free');
function bogo_buy_x_get_y_free($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$buy_product_id = 10;
$free_product_id = 20;
$required_qty = 2;
$buy_qty = 0;
foreach ($cart->get_cart() as $item) {
if ($item['product_id'] == $buy_product_id) {
$buy_qty += $item['quantity'];
}
}
if ($buy_qty >= $required_qty) {
$cart->add_to_cart($free_product_id);
foreach ($cart->get_cart() as $key => $item) {
if ($item['product_id'] == $free_product_id) {
$item['data']->set_price(0);
}
}
}
}
How It Works
- Checks how many units of the main product are in the cart
- If condition is met → adds free product
- Sets price of free product to 0
Important Notes
- Replace product IDs with your own products
- Create a separate product for the free item
- Test with coupons and shipping rules
Customization Ideas
- Limit BOGO offer per user
- Apply only to specific categories
- Add expiration date for promotion
- Show promotional banner on product pages
