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

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

When you sell various products in your online store, such customizations are really useful to encourage customers to buy more, aiming to meet the cart total threshold of a particular product and ultimately unlocking the discount of particular category products.
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();
        }
    }
}

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.

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

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 Unlock a Discount on Category Y Based on the Cart Subtotal of Product X in WooCommerce? - Tyche Softwares



Similarly you can also 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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x