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 seasonal sales are ongoing in your store, you might need to restrict free products for certain items in WooCommerce. But when can this be used might be a question for you?

If you consider the typical scenario, when a customer adds items to their cart, the free product is automatically added. But what if the customer adds an item that is already heavily discounted? In such cases, this WooCommerce customization will help you restrict the free product from being automatically added for those specific product IDs as mentioned in the code is present in the cart.

Solution: Restrict Free Products for Certain Cart Items in WooCommerce

The code snippet will implement BOGO offer restricting the addition of freebies when specific items specified in the code are present in the cart.

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

Keep the momentum going! Explore how BOGO offers can work just right for you, applying them based on various conditions. Enhance your store’s offerings with BOGO deals that automatically add a free product to your WooCommerce cart in 6 different scenarios that will keep your customers excited with smart deals.

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