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

How to Implement Discounts on Category Y Based on Product X Quantity Range in WooCommerce?

One easy tactic to encourage customers to buy and explore items of a particular category that you wish is by providing discounts to those category items. Here, in this customization, based on the quantity range of a specific product, the discounts are applied to the items belonging to a specific category. The below code snippet implements the discounts on items of Category Y such as Kitchen accessories depending on the quantity of a main product like Handy chopper (Product X) added to the cart.

add_action('template_redirect', 'ts_add_product_to_cart');

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

        $product_a_id = 948;
        $product_cat_id = 64;

        // Set the targeted quantity range for the parent product
        $min_targeted_qty = 5;
        $max_targeted_qty = 10;

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

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            // Check if the current product is the parent product
            if ($cart_item['product_id'] == $product_a_id) {
                // Check if the quantity of the parent product falls within the specified range
                if ($cart_item['quantity'] >= $min_targeted_qty && $cart_item['quantity'] <= $max_targeted_qty) {
                    // Loop through cart items and check if there are products from the specified category id
                    foreach (WC()->cart->get_cart() as $inner_cart_item_key => $inner_cart_item) {
                        // Check if the product is in the specified category
                        $product_cats_id_incart = wc_get_product_term_ids($inner_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($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();
        }
    }
}

Output

When the targeted product X is added to the cart and the quantity of this product, when set in between 5 to 10, the code applies a 20% discount to all items of category Y present in the cart.

How to Implement Discounts on Category Y Based on Product X Quantity Range in WooCommerce? - Tyche Softwares

If the quantity of the specific targeted product X doesn’t fall within the specified range of 5 to 10, the code doesn’t imply any discounts for the items related to category Y.


How to Implement Discounts on Category Y Based on Product X Quantity Range in WooCommerce? - Tyche Softwares

Likewise, you can simply add the discount based on the product’s minimum and maximum quantity range without being specific to the category of items. Checkout this guide that will help you to offer a WooCommerce BOGO deal based on the minimum and maximum range of a specific product.

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