How to Set up WooCommerce Buy One Get One Free

WooCommerce Buy One Get One

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.

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:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. 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.
  3. Remember to check the Product IDs.
  4. 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 );
}
}

  1. This is the outcome:free product

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.

Similar Articles

  1. How to Create Number Pagination in WordPress Without Using Plugin
  2. How to Hide Price When Out of Stock In WooCommerce