Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Apply BOGO (Buy One Get One) Discounts to Cart’s Cheapest Item During a Promotion in WooCommerce?

In this guide, we will show you how to set up BOGO discounts via a coupon in WooCommerce, applying the discount to the cheapest item when multiple items are in the cart. With such kinds of promotions, you can target deal-seeking customers and at the same time, you can keep an eye on profit margins as the discount is provided to the cheapest item in the cart.

Solution: Apply BOGO (Buy One Get One) Discounts to Cart’s Cheapest Item During a Promotion

This code implements the ‘Buy One Get One Free’ offer via a coupon by applying the discount to the cheapest item when multiple items are in the cart. Make sure that the defined coupon ‘BOGOF’ is correctly set up in the WooCommerce admin so that it gets automatically applied when the conditions are met.

   add_filter( 'woocommerce_coupon_get_discount_amount', 'ts_filter_wc_coupon_get_discount_amount', 10, 5 );

function ts_filter_wc_coupon_get_discount_amount( $discount_amount, $discounting_amount, $cart_item, $single, $coupon ) {
    // Define below your existing coupon code
    $coupon_code = 'BOGOF';

    // Only for a defined coupon code
    if( strtolower( $coupon_code ) !== $coupon->get_code() ) {
        return $discount_amount;
    }

    $items_prices = [];
    $items_count  = 0;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $key => $item ) {
        // Get the cart item price (the product price)
        if ( wc_prices_include_tax() ) {
            $price = wc_get_price_including_tax( $item['data'] );
        } else {
            $price = wc_get_price_excluding_tax( $item['data'] );
        }

        if ( $price > 0 ) {
            $items_prices[$key] = $price;
            $items_count       += $item['quantity'];
        }
    }

    // Only when there is more than one item in cart
    if ( $items_count > 1 ) {
        asort( $items_prices );  // Sorting prices from lowest to highest

        $item_keys = array_keys( $items_prices );
        $item_key  = reset( $item_keys ); // Get current cart item key

        // Targeting only the current cart item that has the lowest price
        if ( $cart_item['key'] == $item_key ) {
            return reset( $items_prices ); // return the lowest item price as a discount
        }
    } else {
        return 0;
    }
}

flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

The output shows that the specific “BOGOF” coupon is applied while it looks for the cheapest item in the cart and applies the discount to that item. If there is only one item in the cart, it returns 0 as there is no need to apply a discount.

How to Apply BOGO (Buy One Get One) Discounts to Cart's Cheapest Item During a Promotion in WooCommerce? - Tyche Softwares

Let’s also explore another coupon-tied BOGO promotions such as adding a BOGO Buy one get one offer with a coupon code for orders over 100 in WooCommerce.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x