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

How to Add BOGO (Buy One Get One) Offers for Guest Users in WooCommerce?

Offering a BOGO “Buy One Get One” gift to guest users can be a way to encourage them to complete a purchase and create an account on the WooCommerce website. This code snippet adds the free product to the cart only for non-logged-in/guest users enticing these visitors to engage further. The free product is not added for users of any other user roles.

This post only focuses on whether the user is logged-in or not. A user who is not logged-in need not always be a first-time website visitor. Alternatively, we have also written on how you can create buy one get one offer for first time website visitors in WooCommerce.

// Add a free gift for guest users
add_action('woocommerce_before_calculate_totals', 'ts_add_free_gift_for_guests');

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

    // Check if the user is a guest
    if (!is_user_logged_in()) {
        // Get the free gift product ID
        $free_gift_product_id = 470; // Change this to the actual product ID of your free gift

        // Check if the free gift is not already in the cart
        $free_gift_in_cart = false;
        foreach ($cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $free_gift_product_id) {
                $free_gift_in_cart = true;
                break;
            }
        }

        // If the free gift is not in the cart, add it
        if (!$free_gift_in_cart) {
            $cart->add_to_cart($free_gift_product_id);
        }
    }
}

Output

When a non-logged-in user visits your website and adds a product to the cart, the corresponding free product, as defined in the code, is also automatically added to the cart.

How to Add BOGO (Buy One Get One) Offers for Guest Users in WooCommerce? - Tyche Softwares

Let us know how the code was useful or any other queries in the comments.

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