How to Are you looking for a way to add a buy one get one (BOGO) functionality in your store? BOGO or Buy one Get One Free deal is an effective way to increase traffic and make more sales.
You may want to introduce this functionality to clear your inventory.
It is also a cheaper way to build a loyal customer base. You can use coupons but this means you have to send them to your customers. Using coupons is one of the alternatives, considering the cost of online advertising like PPC and social media ads.
However, you can add this functionality using custom PHP code.
Before you proceed, we recommend creating a child theme. This will ensure that your changes are not lost during an update.
If you are not comfortable with handling code, we recommend contacting a qualified WordPress Developer.
Table of Contents
WooCommerce Buy One Get One Without a Plugin
By the end of this brief tutorial, you will be able to add a buy-one-get-one feature on the checkout page.
Read on, as we will define some conditions before we start.
Let us see how you can achieve this.
Steps to Add Buy One Get One Without a Plugin
We will be using product IDs. This means that a gift will be added to the cart if the product ID is added to the cart.
However, it is important to note that the second product should have a price = 0 if you wish to completely give it away.
Alternatively, you can maybe a set sale price. You should set it to “hidden” because maybe you want to hide this free product from the shop and only gift it when the first one is added to Cart.
The code also removes the gifted product if the gifted product is removed from the cart.
Here are the steps you need to follow:
- Log into your WordPress site and access the Dashboard as the admin user.
- From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will add the buy one get one feature.
- Remember to check the Product IDs.
- Add the following code to the PHP file:
/** * @snippet Add Buy One Get One */ add_action( 'template_redirect', 'njengah_add_gift_if_id_in_cart' ); function njengah_add_gift_if_id_in_cart() { if ( is_admin() ) return; if ( WC()->cart->is_empty() ) return; $product_bought_id = 32; $product_gifted_id = 57; // see if product id in cart $product_bought_cart_id = WC()->cart->generate_cart_id( $product_bought_id ); $product_bought_in_cart = WC()->cart->find_product_in_cart( $product_bought_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 not in cart remove gift, else add gift if ( ! $product_bought_in_cart ) { if ( $product_gifted_in_cart ) WC()->cart->remove_cart_item( $product_gifted_in_cart ); } else { if ( ! $product_gifted_in_cart ) WC()->cart->add_to_cart( $product_gifted_id ); } }
Conclusion
By now, you should be able to add a product gift to the cart if a certain product ID is added to the cart.
However, we recommend hiding the product so that customers will not be able to add it to the cart.

Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
More articles written by Joe
Similar Articles
- WooCommerce Redirect After Checkout: Redirect to Custom Thank You Page
- 100+ Tips, Tricks & Snippets Ultimate WooCommerce Hide Guide
- How to Create Number Pagination in WordPress Without Using Plugin
- How to Hide Price and Add to Cart for Logged Out Users WooCommerce
- How to Create WooCommerce Conditional Checkout Fields
- How to Keep WooCommerce Description Tab Open by Default
- How to Import Products WooCommerce
- How to Create a Custom Category Page In WooCommerce
- How to Add Product SKU In WooCommerce
- How to Duplicate a Product in WooCommerce
- How to Create Checkbox Field in WooCommerce Checkout Page
- How to Add Currency to WooCommerce [Custom Currency]
- How to Select All Except Last Child In CSS » CSS Not Last Child Example
- How to Add View Cart Button In WooCommerce
- How to Add Fields in WooCommerce Registration Form
- How to Remove Product Category Title WooCommerce
- How to Add WooCommerce Storefront Product Pagination
- How to Change Cart Subtotal WooCommerce
- How to Create WooCommerce After Login Redirect to Homepage
- How to Hide Price When Out of Stock In WooCommerce