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?

Typical BOGO promotions are applied to an entire product line present in your catalog. But this customization will help you to target specific variation IDs and allows you to offer deals and discounts on precise product variations, such as a particular size, color, or style. This level of targeting ensures that the promotion is relevant to the customer’s preferences. Let’s see how the bogo offerings works for variable products.

Solution: Apply BOGO (Buy One Get One) Offer for Variable Products in WooCommerce

The code snippet applies a 50% discount on every second item of a specific product (with ID 15) in the WooCommerce cart.

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