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

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

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;


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

Alternatively, you can also add 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:

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x