This customization is beneficial when you want your customers to buy more of a specific product to unlock discounts or free items. For e.g. when you have a bundle of products in your online store, and you want to encourage customers to purchase the main product in higher quantities. So, whenever product A is selected in the quantity of 5 units, they get a related accessory (Product B) at half the price.
add_action('template_redirect', 'ts_add_product_to_cart'); function ts_add_product_to_cart() { if (!is_admin()) { global $woocommerce; $product_a_id = 922; $free_product_id = 933; // Set the targeted quantity for the parent product $targeted_qty_a_id = 5; // Targeted quantity for the parent product $discount_percentage = 50; // Discount percentage for the free product // Check if the free product is already in the cart $free_product_cart_id = WC()->cart->generate_cart_id($free_product_id); $free_product_in_cart = WC()->cart->find_product_in_cart($free_product_cart_id); $parent_quantity = 0; // 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) { $parent_quantity = $cart_item['quantity']; break; // No need to continue once we've found the parent product } } // Check if the quantity of the parent product meets the condition if ($parent_quantity == $targeted_qty_a_id) { // If the free product is not in the cart or the cart has been updated, add it with a discount if (!$free_product_in_cart || isset($_POST['update_cart'])) { // Remove the free product from the cart to ensure the discount is applied correctly ts_remove_free_product($free_product_id); // Add the free product to the cart WC()->cart->add_to_cart($free_product_id); // Apply a 50% discount on the added free product foreach (WC()->cart->get_cart() as $key => $cart_item) { if ($cart_item['product_id'] == $free_product_id) { $discount_amount = $cart_item['data']->get_price() * $discount_percentage / 100; $cart_item['data']->set_price($cart_item['data']->get_price() - $discount_amount); } } // Display a notice wc_add_notice('Complimentary gift added! Enjoy a 50% discount on the free product.', 'success'); } } else { // If the quantity condition is not met, remove the free product ts_remove_free_product($free_product_id); } } } function ts_remove_free_product($free_product_id) { global $woocommerce; // Check if free product is in the cart foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if ($_product->get_id() == $free_product_id) { // Remove the free product $woocommerce->cart->remove_cart_item($cart_item_key); // Display a notice wc_add_notice('Complimentary gift removed.', 'notice'); } } }
Output
This is the original price of the complimentary product added to the cart with the discounted amount.
Whenever the parent product with a quantity of 5 is in the cart, the free gift is added with a 50% discount, and the notice reflects this action.
Either if the quantity is increased or decreased and when the quantity of the parent product is not within the specified range then the auto-added product B is removed from the cart.
Similarly, you can also implement BOGO bulk discount tiers that will tie the discounts to a quantity range. Check out here on how to add dynamic bulk WooCommerce discount tiers based on quantity.