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

How to Offer Discount on Category Y Based on the Cart Subtotal of Product X in WooCommerce?

By using such discount rules, you can encourage customers to spend more to meet the cart total threshold associated with a particular product, so they can unlock the discount on products from a particular category.

This can also be used in scenarios where products from certain categories are not moving quickly. In such cases, you can couple them with specific high-value products, which drives more attention and sales to those products. Let’s see how this WooCommerce customization helps us to provide a discount based on cart subtotal.

Solution: WooCommerce Discount Based on Cart Subtotal


The code snippet helps in such scenario: Get category Y on a discount if cart subtotal of Product X exceeds $100.

add_action('template_redirect', 'ts_add_product_to_cart');

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

        $product_a_id = 649; // Targeted Product ID
        $product_cat_id = 63; // Targeted category ID

        // Minimum subtotal of product ID 649 for the discount to apply
        $min_product_subtotal = 100;

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

        // Get the subtotal of product ID 649
        $product_subtotal = 0;
        foreach (WC()->cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $product_a_id) {
                $product_subtotal += $cart_item['line_total'];
            }
        }

        // Check if the subtotal of product ID 649 exceeds the minimum threshold
        if ($product_subtotal >= $min_product_subtotal) {
            // Loop through cart items
            foreach (WC()->cart->get_cart() as $cart_item) {
                // Check if the current product is from the specified category
                $product_cats_id_incart = wc_get_product_term_ids($cart_item['data']->get_id(), 'product_cat');

                if (!is_admin() && in_array($product_cat_id, $product_cats_id_incart)) {
                    // Apply a 20% discount for each item of category 'y' in the cart
                    $discount_percentage = 20;
                    $original_price = floatval($cart_item['data']->get_price());
                    $discount_amount = $original_price * $discount_percentage / 100;
                    $new_price = $original_price - $discount_amount;

                    // Set the new discounted price
                    $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

Promotion TriggerPromotion Details
ConditionTotal cart value reaches or exceeds $100
Targeted ProductSmartphone (Product X)
Targeted CategoryElectronic Accessories (Category ID: 63)
Discount Offered
20% discount in the “Accessories” category

Let’s assume that the promotion item product X is ‘Smartphone’ and the targeted category ID:63 is ‘Electronic Accessories’ that contain various accessories like cases, chargers, and screen protectors, etc.


So, if a customer adds the SmartPhone to their cart (which costs $70), and if the total cart value of product X meets or exceeds $100, then the products from “Accessories” category will get a 20% discount to each accessory in the cart as shown below.

Offer Discount on Category Y

The below image shows that as the cart total value of product X doesn’t meet the specified minimum cart total threshold, the discount is not applied to the items of category Y.

How to Offer Discount on Category Y Based on the Cart Subtotal of Product X in WooCommerce? - Tyche Softwares



Similar to this discount rule, you can also try another effective strategy that offers a BOGO deal. For example, you can set up a promotion like ‘Offer a BOGO Deal: Buy Category X and Get Category Y at a Discount 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