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

WooCommerce Discount: Buy Category X and Get Category Y at a Discount Based on Cart Subtotal

With this WooCommerce customization, you can create a dynamic discount strategy: buy products from Category X and get a discount on items from Category Y based on the cart subtotal. By offering this category-based targeted discount, you’re not just increasing sales in a particular category; you’re creating a shopping experience that feels both rewarding and exciting for your customers.

Solution: WooCommerce Discounts Based on Categories and Cart Subtotal

This code applies a discount to Category Y products when Category X items in the cart meets the subtotal of $200 as defined in the code.

add_action('template_redirect', 'ts_buy_x_get_y_discount');

function ts_buy_x_get_y_discount() {
    if (!is_admin()) {
        global $woocommerce;

        $category_x_id = 60; // Category ID for Category X
        $category_y_id = 16; // Category ID for Category Y
        $min_subtotal = 200; // Minimum subtotal for the discount

        // Flag to check if the discount has been applied
        $discount_applied = false;

        // Calculate cart subtotal
        $cart_subtotal = WC()->cart->subtotal;

        

        // Loop through cart items and apply the discount to Category Y
        foreach (WC()->cart->get_cart() as $inner_cart_item_key => $inner_cart_item) {
            // Check if the product is in Category Y
            $product_cats_id_incart_y = wc_get_product_term_ids($inner_cart_item['data']->get_id(), 'product_cat');

            if (!is_admin() && in_array($category_y_id, $product_cats_id_incart_y)) {
               

                // Apply a discount for each item in Category Y if cart subtotal exceeds $200
                if ($cart_subtotal > $min_subtotal) {
                    $discount_percentage = 10; // You can set the desired discount percentage
                    $original_price = floatval($inner_cart_item['data']->get_price());
                    $discount_amount = $original_price * $discount_percentage / 100;
                    $new_price = $original_price - $discount_amount;

                    // Set the new discounted price
                    $inner_cart_item['data']->set_price($new_price);

                    // Set the flag to indicate that the discount has been applied
                    $discount_applied = true;

                   
                }
            }
        }

        // If the discount is applied, you may need to update the cart totals
        if ($discount_applied) {
            WC()->cart->calculate_totals();
        }
    }
}
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

ScenarioDiscounted/Free Product
Example
Buy Products from Category X and Meet Minimum Cart SubtotalProducts from Category Y at a DiscountIf items from Category X (e.g., electronics) added to the cart meets the cart subtotal exceeds $200, receive a discount on products from Category Y (e.g., accessories)

When the customer adds multiple products to the cart, the discount will only be applied to products in Category Y if there are products from Category X present in the cart and when their subtotal exceeds the specified minimum of $200 as specified in the code.

Buy Category X and Get Category Y at a Discount

On another scenario where the products of category X don’t meet the specified minimum threshold amount then the discount is not applied to the line items of category Y present in the cart.


Alternatively, you can also apply the strategy of both BOGO and discounts together by automatically adding product to cart and applying discount on product B based on cart subtotal 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