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 WooCommerce customization, based on the quantity range of a specific product, the discounts are applied to the items belonging to a specific category. By implementing quantity-based discounts, you can drive more sales and effectively promote specific product categories.

Solution: Implement Discounts on Category Y Based on Product X Quantity Range in WooCommerce

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();
        }
    }
}
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

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.

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.


Likewise, you can simply offer a WooCommerce BOGO deal based on the quantity range of a specific product. without being specific to the category of items.

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