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

How to Offer Discounts on Category Y When Purchasing Category X within a Specified Quantity Range in WooComerce?

This customization is useful while offering bulk promotions to a specific category that relies on the range of number of items bought in a particular category. The code snippet will dynamically apply the discount to products in Category Y (e.g. Electronic Accessories) when a specific quantity range of products from Category X (e.g. Electronics) is added to the cart.

add_action('template_redirect', 'buy_x_get_y_discount');

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

        $category_x_id = 50; // Category ID for Category X
        $category_y_id = 63; // Category ID for Category Y

        $min_qty_x = 5; // Minimum quantity for Category X
        $max_qty_x = 10; // Maximum quantity for Category X

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

        // Count the quantity of items in Category X
        $category_x_qty = 0;

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            // Check if the product is in Category X
            $product_cats_id_incart_x = wc_get_product_term_ids($cart_item['data']->get_id(), 'product_cat');

            if (!is_admin() && in_array($category_x_id, $product_cats_id_incart_x)) {
                // Increment the quantity only if the product is in Category X
                $category_x_qty += $cart_item['quantity'];
            }
        }

        // Check if the quantity condition is met for Category X
        if ($category_x_qty >= $min_qty_x && $category_x_qty <= $max_qty_x) {
            // 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
                    $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();
        }
    }
}

Output

When products from category X (Electronics) are added to the cart the code checks for whether the quantity of items present in this category meets the defined quantity range as given in the code.

  • $min_qty_x = 5; Minimum quantity for Category X.
  •  $max_qty_x = 10; Maximum quantity for Category X.

If it falls within the specified quantity range the discount gets applied to each item of category Y (Electronic Accessories).

How to Offer Discounts on Category Y When Purchasing Category X within a Specified Quantity Range in WooComerce? - Tyche Softwares

If the quantity of the category X items doesn’t meet the specified quantity range then the specified discount amount in the code will not be applied to each item of category Y as shown below.

How to Offer Discounts on Category Y When Purchasing Category X within a Specified Quantity Range in WooComerce? - Tyche Softwares

Regardless of categories, you can also simply offer a WooCommerceBOGO deal based on the minimum and maximum quantity 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