One popular promotional tactic similar to BOGO offers is to give an additional item for free or at a discounted rate upon purchasing a specified quantity of a product. The following code will offer two different free products based on the quantity of the product selected.
add_action('template_redirect', 'ts_add_product_to_cart'); function ts_add_product_to_cart() { if (!is_admin()) { global $woocommerce; $free_product_1_id = 470; // Replace with your free product 1 id $free_product_2_id = 54; // Replace with your free product 2 id $cart_quantity = $woocommerce->cart->cart_contents_count; // Remove free product 1 if already in the cart ts_remove_free_product($free_product_1_id); // Remove free product 2 if already in the cart ts_remove_free_product($free_product_2_id); // Check if cart quantity is between 3 and 6 if ($cart_quantity >= 3 && $cart_quantity <= 6) { // Get the product name for Free Product 1 $free_product_2_name = get_the_title($free_product_2_id); wc_add_notice("If you select beyond 6, you will get $free_product_2_name for free.", 'notice'); // Add Free Product 1 ts_add_free_product($free_product_1_id); } elseif ($cart_quantity >= 8 && $cart_quantity <= 12) { // Add Free Product 2 ts_add_free_product($free_product_2_id); } } } function ts_add_free_product($free_product_id) { global $woocommerce; // Check if free product not already in cart if (!is_free_product_in_cart($free_product_id)) { $woocommerce->cart->add_to_cart($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); } } } function is_free_product_in_cart($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) { return true; } } return false; }
Output
Based on the cart quantity of the selected product, the code adds a free product under certain conditions:
- If the cart quantity is between 3 and 6, it adds the first free product ($free_product_1_id) and also displays a notice message as shown in the image below.
If the cart quantity is between 8 and 12, it adds the second free product ($free_product_2_id).
Similarly, you can also add free products based on cart total. Look into this post that guides you well on how to automatically add a product to your WooCommerce cart based on 6 different conditions including cart total.