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?

While offering BOGO discounts, it is essential to manage these promotions as some WooCommerce products that are currently on sale have already been discounted from their regular prices. So, while providing storewide offers, you can exclude providing offers to these on-sale products. Overall, you can attract customers who are looking for immediate discounts and those who purchase more to avail the free offer.

Solution: Exclude BOGO (Buy One Get One) Offer for On-Sale Products in WooCommerce


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

Similar to excluding on-sale products, if you have high-value items and prefer not to apply BOGO offers to them, you can use the BOGO rule in WooCommerce: “Restrict Free Products for Certain Cart Items in WooCommerce“. This rule ensures that specific product IDs will not receive free gifts when added to the cart, allowing you to maintain pricing strategies for your premium items.

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