This customization will provide ‘Category-Based Discounts’ encouraging customers to buy in bulk quantities from one category so that you can highlight a discount in another category. This strategy not only entices customers with appealing offers but also effectively drives higher sales volumes and increases the visibility of targeted categories.
Solution: WooCommerce Buy X (in specified Quantity Range) Get Y discount by 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 (e.g. 5 to 10 as specified in the code) 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
Scenario | Discounted Product | Example |
Buy products from Category X (Electronics) with a quantity range between 5 and 10 | Discount on products from Category Y (Electronic Accessories) | Buy 5 to 10 units of items like smartphones from Electronics(category X) to get a discount on accessories like chargers from Electronic Accessories(category Y). |
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).
![WooCommerce Buy X Get Y discount by Category 1 WooCommerce Buy X Get Y discount by Category - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/discount-on-category-y-items-based-on-quantity-range-of-x-items-1024x476.png)
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.
![WooCommerce Buy X Get Y discount by Category 2 WooCommerce Buy X Get Y discount by Category - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/discoutn-not-applied-for-quantity-range-of-category-x-items-1024x464.png)
Move the traditional discounts aside and start offering complimentary gift automatically added to the cart when they purchase a specific quantity of products. This simple rule will be exciting and will also offer a free product based on the minimum and maximum quantity range of a specific product.