Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Exclude BOGO (Buy One Get One) Offer for On-Sale Products in WooCommerce?

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.

How to Exclude BOGO (Buy One Get One) Offer for On-Sale Products in WooCommerce? - Tyche Softwares
How to Exclude BOGO (Buy One Get One) Offer for On-Sale Products in WooCommerce? - Tyche Softwares

Let’s continue adding a normal product that is not an on-sale to the cart.

How to Exclude BOGO (Buy One Get One) Offer for On-Sale Products in WooCommerce? - Tyche Softwares

When a product that is not on sale is added to the cart, the free gift also gets automatically added to the cart items.

How to Exclude BOGO (Buy One Get One) Offer for On-Sale Products in WooCommerce? - Tyche Softwares

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x