Buy 3 get 1 free coupon WooCommerce

add buy 3 get one free coupon woocommerce

How to add Buy 3 get 1 free coupon in WooCommerce

If you wanted to add this functionality to WooCommerce you probably discovered that the official WooCommerce Dynamic Pricing plugin doesn’t support it. Or maybe you tried Pricing Deals for WooCommerce and after 7 hours figuring out how it works you discovered that the coupon solution is not AJAX and it needs a manual refresh by the customer. Unacceptable.

You’re in luck since I will share a custom solution for you that can be adapted to your needs. With this coupon you can buy one get one free, buy 3 get one free or buy as many as you want and get the cheapest product in the care free.

Add this code in your functions.php file to get buy 3 get one free (the cheapest).

// Hook before calculate fees - "Buy 3 get cheapest free" coupon
add_action('woocommerce_cart_calculate_fees' , 'buy3_coupon');

/**
 * Add discount for "Buy 3 get cheapest free" coupon
 * @param WC_Cart $cart
 */

function buy3_coupon( WC_Cart $cart ){

	// add the coupons here
	$buy3_coupons = array('onefree', 'anothercouponcode');

	// return if cart has less than 4 items
	if( $cart->cart_contents_count < 3 ){
	    return;
	}

	$applied_coupons = $cart->get_applied_coupons();	
	$matches = array_intersect($buy3_coupons, $applied_coupons);

	// return if no coupon matches
	if (empty($matches)) return;
	
	// loop through the items in cart to find the cheapest
	foreach ( $cart->get_cart() as $cart_item_key => $values ) {
		$_product = $values['data'];
		$product_price[] = $_product->get_price_including_tax();
	}
	
	$cheapest = min($product_price);

    $cart->add_fee( 'Coupon deal: Buy 3 get 1 for free', -$cheapest);
}

Add the coupon in WooCommerce

Add the coupon and ensure the sum is set to 0.
add buy 3 get one free coupon woocommerce

Under usage restrictions click Check this box if the coupon cannot be used in conjunction with other coupons and click Exclude sale items. These are optional.
add coupon usage restrictions

Go test the coupon
buy 3 get one free coupon cart woocommerce

10 Responses to “Buy 3 get 1 free coupon WooCommerce”

  1. wow just what I needed.
    we have a set of 5 products that when you buy 3 you get the fourth free.
    I’m just having a little trouble getting it to apply automaicially

    I added it to the child theme functions php. it doesn’t automatically apply to the products sofar, but works if I add it to the coupon field.

    But it’s a great solution. I was about to get the plugins above glad you wrote this!
    Mark

  2. Hey Mark,
    I’m glad it’s been helpful to you. Yes this solution is tied to the coupons usage.

  3. Hi Lucian,

    I set the coupon to apply to 5 specific product ids. But when I add 4 of those products to the cart it doesn’t automatically trigger the coupon.
    But it should, right?

  4. The coupon needs to be added manually by the customer. The discounts are applied automatically. This is a coupon based solution

  5. Hi thanks for this!

    I have made an offer ‘Buy 4 get an additional free’

    Therefore when a customer adds 5 items to the basket and puts the code in it should deduct the 5th cheapest item, i have changed the coding manually but cant get it to work, may i have help with this one please?

    Thanks

    Hannah

  6. You’ll want to play with the number from this code:
    // return if cart has less than 4 items
    if( $cart->cart_contents_count < 3 ){ return; } then adjust the wording here: $cart->add_fee( ‘Coupon deal: Buy 3 get 1 for free’, -$cheapest);

  7. Hi, I was looking for this a long time and I followed your steps. But I’m a bit panicked now. Everything works great , you get the discount in the cart but in the checkout, the original price is charged. Newsletter has been sent out to customers and they observed this error and contacted us. Any suggestions please? I’m desperate
    Best regards

  8. I’ve tested it on my end and it works fine on the checkout. It may be a theme issue:
    https://www.dropbox.com/s/lnzett4mk2pnc68/Screenshot%202016-12-15%2017.27.06.png?dl=0

  9. Not sure but do you know a code for BUY ONE, GET ONE HALF OFF

  10. This code is purposed to give away the cheapest product off. The 50% discount would require another logic.