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

How to Create BOGO (Buy One Get One) Offers for First Time Website Visitors in WooCommerce?

Do you want to provide a BOGO ‘Buy One Get One’ offer for a specific product to the WooCommerce cart during a customer’s first visit to the website, then this snippet is the solution for you.

add_action( 'template_redirect', 'ts_add_product_to_cart_on_first_visit' );

function ts_add_product_to_cart_on_first_visit() {
    if ( ! is_admin() ) {
        $product_id = 34;

        // Check if it's the user's first visit
        if ( ! isset( $_COOKIE['first_visit'] ) ) {
            // Set a cookie to track the user's visit
            setcookie( 'first_visit', '1', time() + ( 30 * 1 * 60 * 60 ), '/' );

            // Check if the product is not already in the cart
            $found = false;
            if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
                foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                    $_product = $values['data'];
                    if ( $_product->get_id() == $product_id ) {
                        $found = true;
                        break;
                    }
                }
            }

            // If the product is not found, add it to the cart
            if ( ! $found ) {
                WC()->cart->add_to_cart( $product_id );
            }
        }
    }
}

Output

When the customer visits the website, then the free product i.e. ‘Hoodie with Logo” will get automatically added to the WooCommerce cart page.

How to Create BOGO Offers for First Time Website Visitors in WooCommerce

In another scenario, if the customer visits the website for the second time, then the free product (Tshirt with logo) will not be added to the cart.

How to Create BOGO Offers for First Time Website Visitors in WooCommerce

Additionally, you can also apply BOGO (Buy One Get One) offer for variable products in WooCommerce cart page.

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