With this WooCommerce customization, you can create a dynamic discount strategy: buy products from Category X and get a discount on items from Category Y based on the cart subtotal. By offering this category-based targeted discount, you’re not just increasing sales in a particular category; you’re creating a shopping experience that feels both rewarding and exciting for your customers.
Solution: WooCommerce Discounts Based on Categories and Cart Subtotal
This code applies a discount to Category Y products when Category X items in the cart meets the subtotal of $200 as defined in the code.
add_action('template_redirect', 'ts_buy_x_get_y_discount'); function ts_buy_x_get_y_discount() { if (!is_admin()) { global $woocommerce; $category_x_id = 60; // Category ID for Category X $category_y_id = 16; // Category ID for Category Y $min_subtotal = 200; // Minimum subtotal for the discount // Flag to check if the discount has been applied $discount_applied = false; // Calculate cart subtotal $cart_subtotal = WC()->cart->subtotal; // 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 if cart subtotal exceeds $200 if ($cart_subtotal > $min_subtotal) { $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/Free Product | Example |
Buy Products from Category X and Meet Minimum Cart Subtotal | Products from Category Y at a Discount | If items from Category X (e.g., electronics) added to the cart meets the cart subtotal exceeds $200, receive a discount on products from Category Y (e.g., accessories) |
When the customer adds multiple products to the cart, the discount will only be applied to products in Category Y if there are products from Category X present in the cart and when their subtotal exceeds the specified minimum of $200 as specified in the code.
![WooCommerce Discount: Buy Category X and Get Category Y at a Discount Based on Cart Subtotal 1 WooCommerce Discount: Buy Category X and Get Category Y at a Discount Based on Cart Subtotal - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/Discount-on-category-Y-when-met-the-cart-subtotal-threshold-1024x748.png)
On another scenario where the products of category X don’t meet the specified minimum threshold amount then the discount is not applied to the line items of category Y present in the cart.
![WooCommerce Discount: Buy Category X and Get Category Y at a Discount Based on Cart Subtotal 2 WooCommerce Discount: Buy Category X and Get Category Y at a Discount Based on Cart Subtotal - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/Discount-not-applied-when-products-of-category-X-not-neet-subtotal-threshold-1024x696.png)
Alternatively, you can also apply the strategy of both BOGO and discounts together by automatically adding product to cart and applying discount on product B based on cart subtotal in WooCommerce.