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

How to Implement Buy 2 Get 50% Off on Second Item in WooCommerce?

This WooCommerce customization will help store owners to easily implement ‘Buy 2 Get 50% off’ on the second item purchased. Offering such attractive promotions like BOGO (Buy One, Get One) or discounted second items all these leads to increase sales and draw in more customers to your store.

Solution: Implement Buy 2 Get 50% Off on Second Item in WooCommerce

This code helps in providing BOGO offers such as “Buy one, get one 50% off” discount on a specific product when added multiple times to the WooCommerce cart, applying the discount dynamically based on quantity.

add_action('woocommerce_cart_calculate_fees', 'ts_custom_discount_2nd_at_50', 10, 1 );
function ts_custom_discount_2nd_at_50( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    // YOUR SETTINGS:
    $targeted_product_id = 34; // Set HERE your targeted product ID

    // Initializing variables
    $discount = $qty_notice = 0;
    $items_prices = array();

    // Loop through cart items
    foreach ( $cart->get_cart() as $key => $cart_item ) {
        if( in_array( $targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ){
            $quantity = (int) $cart_item['quantity'];
            $qty_notice += $quantity;
            for( $i = 0; $i < $quantity; $i++ ) {
                $items_prices[] = floatval( $cart_item['data']->get_price());
            }
        }
    }

    $count_items = count($items_prices); // Count items

    rsort($items_prices); // Sorting prices descending order

    if( $count_items > 1 ) {
        foreach( $items_prices as $key => $price ) {
            if( $key % 2 == 1 )
                $discount -= number_format( $price / 2, 2 );
        }
    }

    // Applying the discount
    if( $discount != 0 ){
        $cart->add_fee('Buy one get one 50% off', $discount );

        // Displaying a custom notice (optional)
        wc_clear_notices(); // clear other notices on checkout page.
        if( ! is_checkout() ){
            wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
        }
    }
    //  Display a custom notice on cart page when quantity is equal to 1.
    elseif( $qty_notice == 1 ){
        wc_clear_notices(); // clear other notices on checkout page.
        if( is_cart() ){
            wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
        }
    }
}
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


When a customer adds the same item twice with a quantity of 2 on the WooCommerce cart page, a 50% discount is applied to the second item. This discount is then applied to the cart total, and a notice is displayed to the customer indicating that the discount has been applied.

Buy 2 Get 50% Off

Similar to this customization, you can also set up discounts that follows a consistent pattern like offering a 50% discount on every third purchase of product A 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