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?

A “Buy 2 Get 50% Off” discount for specific product categories is an effective way to incentivize customers to purchase more items, especially within particular categories that you want to promote. This guide will walk you through the process of setting up such a discount in WooCommerce, ensuring a seamless and attractive offer for your customers.

Solution: Apply Buy 2 get 50% Discount for Specific Product Categories in WooCommerce

The code snippet applies a 50% discount to the second item (and any additional items) of products from specified categories in a WooCommerce cart. It ensures that only items in the defined categories and with a quantity greater than one receive this discount.

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

Discover a variety of BOGO strategies to elevate your product-based promotions. For example, you can 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