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

How to Apply BOGO (Buy One Get One) For Registered User in WooCommerce ?

Setting up a “Buy One Get One” (BOGO) deal or WooCommerce cart discount exclusively for registered users will provide long-term benefits beyond immediate sales. While every store owner often puts some effort into getting customer details, offering such exclusive discount deals will help you turn guest shoppers into registered users. Let’s see how the discount is applied for the second item added to the cart.

Solution: Apply BOGO (Buy One Get One) For Registered User in WooCommerce

The code applies a custom discount of 50% on the second item of a specific product in the WooCommerce cart for registered users. It checks the cart for the targeted product ID, calculates the discount based on the product prices, and applies it as a fee named “Buy one get one 50% off” during checkout.

add_action('woocommerce_cart_calculate_fees', 'ts_custom_discount_2nd_at_50', 10, 1);

function ts_custom_discount_2nd_at_50($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // Check if the user is logged in
    if (is_user_logged_in()) {

        // YOUR SETTINGS:
        $targeted_product_id = 132; // Set HERE your targeted product ID

        // Initializing variables
        $discount = $qty_notice = 0;
        $items_prices = array();

        // Loop through cart items
        foreach ($cart->get_cart() as $key => $cart_item) {
            if (in_array($targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']])) {
                $quantity = (int)$cart_item['quantity'];
                $qty_notice += $quantity;
                for ($i = 0; $i < $quantity; $i++) {
                    $items_prices[] = floatval($cart_item['data']->get_price());
                }
            }
        }

        $count_items = count($items_prices); // Count items

        rsort($items_prices); // Sorting prices descending order

        if ($count_items > 1) {
            foreach ($items_prices as $key => $price) {
                if ($key % 2 == 1)
                    $discount -= number_format($price / 2, 2);
            }
        }

        // Applying the discount
        if ($discount != 0) {
            $cart->add_fee('Buy one get one 50% off', $discount);

            // Displaying a custom notice (optional)
            wc_clear_notices(); // clear other notices on checkout page.
            if (!is_checkout()) {
                wc_add_notice(__("You get 50% of discount on the 2nd item"), 'notice');
            }
        }
        // Display a custom notice on cart page when quantity is equal to 1.
        elseif ($qty_notice == 1) {
            wc_clear_notices(); // clear other notices on checkout page.
            if (is_cart()) {
                wc_add_notice(__("Add one more to get 50% off on 2nd item"), 'notice');
            }
        }
    }
}

Output

When the customer logs in and adds the product(storage box) to their cart with a quantity of 2, then the second item will receive a 50% discount, as shown below.

How to Apply BOGO For Registered User in WooCommerce Cart

In another case, if the customer is not registered, the BOGO offer will not be applied to the cart page as shown below.

How to Apply BOGO For Registered User in WooCommerce Cart

Registered users are often repeat purchasers, who adds valuable to your business. Applying BOGO (Buy One Get One) discounts based on customer roles in the WooCommerce cart page is a quick and effective strategy to attract more customers and boost sales.

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