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

How to Offer BOGO Deals Based on Customer Email Domain in WooCommerce?

This post helps you to provide BOGO offers based on customer email domains. Offering BOGO offers to selected email domains gives a sense of exclusivity to those customers who are part of that email domain. Additionally, the code also sends an email notification about the added free gift.

function ts_check_and_notify_free_product_added( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    // Define the allowed email domains
    $allowed_email_domains = array( 'gmail.com', 'yahoo.com' );

    // Get the user's email address
    $user_id = get_current_user_id();
    $user_email = get_userdata($user_id)->user_email;

    // Define the free product ID
    $free_product_id = 470;

    // Check if the free product is not already in the cart and another product is being added
    if (!ts_wc_find_product_in_cart($free_product_id) && !empty(WC()->cart->get_cart_contents())) {
        // Add the free product to the cart
        WC()->cart->add_to_cart($free_product_id, 1);

        // Send email notification
        $to = $user_email;
        $subject = 'Free Product Added to Your Cart';
        $message = 'Hello, 
                    The free product with ID ' . $free_product_id . ' has been added to your cart. 
                    Thank you for shopping with us!';
        
        // Display a notice to the user
        wc_add_notice('Congratulations! You have received a free product. Check your cart for details.', 'success');
        
        // Send email
        wp_mail($to, $subject, $message);
    }
}

add_action('woocommerce_add_to_cart', 'ts_check_and_notify_free_product_added', 10, 6);

// Helper function to find a product in the cart
function ts_wc_find_product_in_cart($product_id) {
    foreach (WC()->cart->get_cart() as $cart_item) {
        if ($product_id == $cart_item['product_id']) {
            return true;
        }
    }
    return false;
}

// Helper function to check if an email address has an allowed domain
function ts_email_has_allowed_domain($email, $allowed_domains) {
    $domain = substr(strrchr($email, "@"), 1);
    return in_array($domain, $allowed_domains);
}

Output

Let’s say when a customer logs in from a particular email domain such as ‘@gmail.com’, and then adds any product to the cart, the free product is automatically added to the cart. In case of logging in using any other email domain, the free product will not be offered.

How to Offer BOGO Deals Based on Customer Email Domain in WooCommerce? - Tyche Softwares

The customer also receives an email notification about the details of the added free gift as shown below.

How to Offer BOGO Deals Based on Customer Email Domain in WooCommerce? - Tyche Softwares

Similarly, you can also add bogo buy one get one offer for registered user in WooCommerce cart that will exclusively work only for non-logged-in users.

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