
How to Programmatically Add Free Gifts in WooCommerce (No Plugin Required)
In this post, I’ll show you how to implement a free gift (Buy X, Get Y) promotion in WooCommerce purely with code—no plugin required. Using the woocommerce_payment_complete
hook, you can automatically add a free product to an order once a qualifying purchase is completed.
This technique gives you full control and flexibility over your promotional campaigns, and can even be extended to support advanced features like product bundles or tiered rewards.
Use Case: Buy X, Get Y for Free
“Buy X, Get Y” promotions are very common in e-commerce. While there are several plugins available for WooCommerce that provide this functionality, implementing it manually offers greater customization and avoids plugin bloat.
Core Code to Add a Free Gift
In the code below, we hook into woocommerce_payment_complete
, which is triggered when a customer successfully completes payment.
If the order contains a product with ID 123
(Product X), the script automatically adds another product with ID 456
(Product Y) to the order as a free gift by setting its price to zero.
add_action('woocommerce_payment_complete', 'wprs_add_products_to_order', 9999);
/**
* Add a free gift to the order if a specific product is purchased.
*
* @param int $order_id
* @return void
*/
function wprs_add_products_to_order($order_id)
{
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
if ($product_id && $product_id == 123) { // Replace 123 with your qualifying product ID
$order->add_product(wc_get_product(456), 1, [ // Replace 456 with your gift product ID
'subtotal' => 0,
'total' => 0
]);
$order->save();
// Optional: grant downloadable access for the gift product
// wc_downloadable_product_permissions($order_id, true);
// Optional: reduce gift product stock
// wc_update_product_stock(wc_get_product(456), 1, 'decrease');
break; // Prevent multiple gifts if product is added multiple times
}
}
}
Dynamic Gift Rules
The product IDs 123
and 456
are hardcoded in the example for simplicity. However, in a real-world application, you can store these values in custom product fields (e.g., using ACF or custom meta fields) to make the gift rules more dynamic and easier to manage through the WordPress dashboard.
Extend the Idea: Product Bundles
This approach doesn’t just apply to free gifts—you can also use similar logic to create simple product bundles. For instance, when a customer buys a base product, the system can automatically include a set of predefined items in the order. This is a powerful way to create custom bundles or kits tailored to your marketing strategy.
Final Thoughts
With a few lines of PHP, you can build your own gift or bundling system in WooCommerce without the need for extra plugins. This gives you full control over the behavior, design, and performance of your store’s promotional logic.
Here are a few ways to expand on this foundation:
- Add multiple gift rules for different products
- Use custom fields or a settings page to manage gifts
- Customize email notifications to mention the free gift
- Apply different gifts based on total order value or quantity
WooCommerce is highly extensible, and with hooks like woocommerce_payment_complete
, the possibilities for customization are nearly endless.