While running Buy One Get One promotion in WooCommerce stores, it’s crucial to manage the number of free products each customer receives. This WooCommerce customization will control BOGO “Buy One Get One” promotions for each user. It sets a limit on how many free products a user can get and keeps track of the count. Once a user reaches the set limit, they’ll receive a notice saying they’ve hit the maximum and can’t get more free products. Let’s dive in to know how it works!
Solution: Limit Free Products For User When Offering BOGO(Buy One Get One) Offers
This code snippet tracks the number of free products each user has received and adds a free product only if they haven’t already reached the predefined limit of 5 free products. If the user exceeds the limit, they receive an error notice.
add_action('woocommerce_before_calculate_totals', 'ts_add_free_product_to_cart'); function ts_add_free_product_to_cart($cart) { // Check if the user is logged in $user_id = get_current_user_id(); if ($user_id > 0) { // Get the user meta field to track the number of times the free product has been added $free_product_count = get_user_meta($user_id, 'ts_free_product_count', true); $free_product_count = intval($free_product_count); // Define the maximum number of times the free product can be added (5 times in this case) $max_free_product_count = 5; // Define the free product ID $free_product_id = 470; // Replace with the actual ID of your free product // Check if the free product is not already in the cart $free_product_in_cart = false; foreach ($cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $free_product_id) { $free_product_in_cart = true; break; } } // If the free product is not in the cart and the user is eligible (not exceeded the limit), add it if (!$free_product_in_cart && $free_product_count < $max_free_product_count) { $cart->add_to_cart($free_product_id); // Increment the free product count for the user $free_product_count++; update_user_meta($user_id, 'ts_free_product_count', $free_product_count); } elseif (!$free_product_in_cart && $free_product_count >= $max_free_product_count && !isset(WC()->session->added_notice)) { // Add a notice if the maximum usage limit is exceeded and the free product is not in the cart wc_add_notice(__('Maximum usage limit exceeded for getting the free product', 'your-text-domain'), 'error'); // Set a session variable to mark that the notice has been added WC()->session->added_notice = true; } } }
This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…
BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?
Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.
Output
Let’s say you have set a BOGO promotion limit to 5 as defined in the code($max_free_product_count = 5) for each user. When the user makes a purchase, he gets a free product. Later on, when the user makes four more purchases, each time they will get a free product as shown below.
But as the limit of free products to each user is set to 5 in the code, when the user attempts to make another purchase on the 6th time to qualify for a free product, the code will generate a notice message.
In this post, we have seen how to restrict the allocation of the number of free products offered to each user. Alternatively, you can also apply the BOGO buy one get one offer based on user roles as well. For instance, offering free products to VIP customers can incentivize more customers to join the VIP group, attracted by the rewards of gifts.