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

How to Apply Buy 2 get 50% Discount for Specific Product Categories in WooCommerce?

Similar to BOGO promotional discounts this customization encourages customers to buy multiple items from specific categories. This code provides a discount of 50% on the second item of each item in the cart if the item quantity is greater than 1 and it belongs to one of the specified product categories.

add_action( 'woocommerce_cart_calculate_fees', 'ts_second_item_discount', 10, 1 );
function ts_second_item_discount( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 50; // 50%
    $category_ids = array( 50, 16 ); // Defined product categories term IDs
    $discount   = 0;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // When quantity is more than 1 and for defined product categories
        if( $cart_item['quantity'] > 1 && array_intersect( $category_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) ) ){
            // 20% of the product price as a discount
            $discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
        }
    }
    if( $discount > 0 )
        $cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}

Output

The category IDs specified in the code are the categories of ‘Electronics’ and ‘Accessories’. So when products belonging to these items are added to the cart, the code implies to add a 50% discount for every second item.

How to Apply Buy 2 get 50% Discount for Specific Product Categories in WooCommerce? - Tyche Softwares

Likewise, you can also provide a free gift based on product tags. Refer to this post that will help you to apply bogo buy one get one offer based on product tags in WooCommerce cart.

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