This customization is useful when you want to leverage the selling of a particular category by providing discounts. The code snippet allows you to offer a discount on all items in a specific category based on the quantity of a designated product (Product A) in the cart.
Let’s consider an example for an online electronics store selling the product Smartphone and the store also has a category called “Accessories” (Category ID: 16) that includes various items such as phone cases, screen protectors, and chargers. If the smartphone is bought exactly 4 in quantities then a 20% discount is applied to all line items of these categories present in 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 = 100; $product_cat_id = 16; // Set the targeted quantity for the parent product $targeted_qty_a_id = 4; // Targeted quantity // 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 meets the condition if ($cart_item['quantity'] == $targeted_qty_a_id) { // 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 product smartphone is added to the cart and if the quantity of this specific product is selected in equal to the targeted quantity 4, then the code loops through the cart items and checks if there are products from the specified category ID: 16. If found any, then the 20% discount is applied to such items.
Before seeing the output let’s look into the original prices of the products belonging to the category ‘Electronic Accessories’ as this will help to compare the prices of the discounted amount later.
Below is the output of the items that meets the criteria for the discount to be applied, since the quantity of Product A meets the condition of the targeted quantity of 4 and therefore the discount is applied to the items of category ‘Electronic Accessories’.
If the quantity of product A is less than 4, then the discount will not be applied to the products assigned to the category ‘Electronic Accessories’.
Alternatively, you can offer a free product from different categories. Check out this post that will help to set up a BOGO deal: buy one get one free from different categories in WooCommerce.