WooCommerce products that are currently on sale have already been discounted from their regular prices. So, while providing BOGO offers storewide, you can exclude providing offers to these on-sale products. This code will add a free product for all products except when on-sale products are added to the cart.
function ts_add_free_product_to_cart( $item_key, $product_id ) { $free_product_id = 470; // Product Id of the free product which will get added to cart $found = false; // Check if the product is on sale $product_is_on_sale = get_post_meta( $product_id, '_sale_price', true ) !== ''; // Check if the product is not on sale before adding it to the cart if ( ! $product_is_on_sale ) { // Check if the free product is already in the cart if ( sizeof( WC()->cart->get_cart() ) > 0 ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if ( $_product->get_id() == $free_product_id ) { $found = true; } } // If the free product is not found, add it to the cart if ( ! $found ) { WC()->cart->add_to_cart( $free_product_id ); } } else { // If no products in the cart, add the free product WC()->cart->add_to_cart( $free_product_id ); } } } add_action( 'woocommerce_add_to_cart', 'ts_add_free_product_to_cart', 10, 2 );
Output
The below output shows that the free products are not added to the cart, when an on-sale product is added to the cart.
Let’s continue adding a normal product that is not an on-sale to the cart.
When a product that is not on sale is added to the cart, the free gift also gets automatically added to the cart items.
Similarly, you can apply bogo buy one get one offer based on product tags in WooCommerce cart that will exclusively add free gifts when specific product tags are added to the cart.