By Implementing such BOGO deals to your store, you can incentive your customers to purchase bundled items. For example, you can offer an “Electronic Bundle Deal.” Customers who purchase a TV and a stabilizer together will receive a high-quality wireless speaker for free.
add_action( 'template_redirect', 'ts_add_gift_id_in_cart' ); function ts_add_gift_id_in_cart() { if ( is_admin() ) return; if ( WC()->cart->is_empty() ) return; $product_a_id = 100; $product_b_id = 457; $product_gifted_id = 470; // see if product A in cart $product_a_cart_id = WC()->cart->generate_cart_id( $product_a_id ); $product_a_in_cart = WC()->cart->find_product_in_cart( $product_a_cart_id ); // see if product B in cart $product_b_cart_id = WC()->cart->generate_cart_id( $product_b_id ); $product_b_in_cart = WC()->cart->find_product_in_cart( $product_b_cart_id ); // see if gift id in cart $product_gifted_cart_id = WC()->cart->generate_cart_id( $product_gifted_id ); $product_gifted_in_cart = WC()->cart->find_product_in_cart( $product_gifted_cart_id ); // if both products A and B are in cart, add gift C if ( $product_a_in_cart && $product_b_in_cart ) { if ( ! $product_gifted_in_cart ) { WC()->cart->add_to_cart( $product_gifted_id ); } } else { // if not both products A and B are in cart, remove gift C if present if ( $product_gifted_in_cart ) { WC()->cart->remove_cart_item( $product_gifted_in_cart ); } } }
Output
The free product gets automatically added to the cart, only when a customer purchases both products A and B. The free product doesn’t get added if only one of the specified products is added to the cart.
Similarly, you can also refer to this post and implement buy 1 get 2 free action in WooCommerce.