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

How to Restrict Free Products for Certain Cart Items in WooCommerce?

When storewide offers such as BOGO “Buy One get One ” or some seasonal sales are ongoing in your store, you might have the need to restrict such offers to certain products. In such cases, this code will help you to restrict the free product from getting added only for specific product IDs mentioned in the code.

add_action('wp_loaded', 'ts_bogo_offer');

function ts_bogo_offer() {
    // Check if WooCommerce is active
    if (class_exists('WooCommerce') && is_object(WC()->cart)) {
        // Define the product IDs for comparison
        $product_gifted_id = 470;
        $restricted_product_ids = array(100, 457);

        // Check if any restricted product is in the cart
        $restricted_product_in_cart = false;
        foreach (WC()->cart->get_cart() as $cart_item) {
            $product_id = $cart_item['product_id'];
            if (in_array($product_id, $restricted_product_ids)) {
                $restricted_product_in_cart = true;
                break;
            }
        }

        // See if the gift ID is in the 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 any restricted product in cart, remove gift; else, add gift
        if ($restricted_product_in_cart) {
            if ($product_gifted_in_cart) {
                WC()->cart->remove_cart_item($product_gifted_in_cart);
            }
        } else {
            if (!$product_gifted_in_cart) {
                WC()->cart->add_to_cart($product_gifted_id);
            }
        }
    }
}

Output

The below image shows the restricted products that I have mentioned in the code. When these products are in the cart, the BOGO offer will not be applied in any case.

How to Restrict Free Products for Certain Cart Items in WooCommerce? - Tyche Softwares

When Other Products Are Added (Allowed Products):

If a customer adds any product to the cart from the allowed product IDs, the free product gets added to the cart.

How to Restrict Free Products for Certain Cart Items in WooCommerce? - Tyche Softwares

When Restricted Product IDs Are Added:

If a customer adds a product to the cart with a product ID that is in the restricted list, the code will remove any free products that was added previously from the cart.

How to Restrict Free Products for Certain Cart Items in WooCommerce? - Tyche Softwares

Similarly, you can apply bogo offers based on ‘N’ number of conditions. Check out this guide that will help you to automatically add a free product to your WooCommerce cart in 6 different scenarios.

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