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) Offer for Variable Products in WooCommerce?

Do you want to apply a BOGO ‘buy one get one’ offer for variable products in the WooCommerce cart page based on the product IDs? Here is the solution for you.

add_action('woocommerce_cart_calculate_fees', 'ts_add_custom_discount_2nd_at_50', 10, 1 );
function ts_add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();

    // Set here your targeted variable product ID
    $targeted_product_id = 15;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 1 ) foreach( $items_prices as $key => $price )
        if( $key % 2 == 1 ) $discount -= number_format($price / 2, 2 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
    }
}

Output

When the customer adds two quantities of Hoodie-Red items to the WooCommerce cart page, the discount applies to the second item as shown below.

BOGO Offer for Variable Products in WooCommerce

Similarly, you can also implement buy 2 get 50% off on second item in WooCommerce cart page.

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

Share It:

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