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

How To Send Email Notification and Add a Free Gift for an Abandoned Cart?

While sending reminder emails to customers about their abandoned cart is one of the strategies used by store owners, why not surprise them with a free gift and send them the gift details in email as well? This is one of the best way to make your customers complete their orders.

This post will guide you to recover abandoned carts for logged-in users without requiring the need to install any Abandoned cart recovery plugin.

Solution: Send Email Notification and Add a Free Gift for an Abandoned Cart

The code works by checking the last cart updated time and if the cart has not been updated for the designated 5-minute interval, then a reminder email notification is sent to the logged-in user. When the user visits his cart again, the free gift will be automatically added to the cart. Either if the cart is updated or if the order gets placed within this time interval the notification is stopped from being sent.

add_action('wp_loaded', 'capture_abandoned_cart_email');

function capture_abandoned_cart_email() {
    // Check if an order has been placed since the last cart update
    if (get_transient('order_placed_after_last_cart_update')) {
        return; // If an order has been placed, exit the function
    }

    // Check if the cart has been updated within the last 5 minutes
    $last_cart_update_time = get_transient('last_cart_update_time');

    if (empty($last_cart_update_time) || (time() - $last_cart_update_time > 300)) {
        // Set the last cart update time to the current time
        set_transient('last_cart_update_time', time());

        // Capture abandoned cart email logic here
        $user_email = '';

        // Check if the user is logged in
        if (is_user_logged_in()) {
            $current_user = wp_get_current_user();
            $user_email = $current_user->user_email;
            add_free_product_to_cart(); // Add the free product to the cart
        } 
        // Check if the user's email is available
        if (!empty($user_email)) {
            // Compose the subject and message for the abandoned cart email
            $subject = 'Your Cart is Waiting!';
            $message = 'Dear user, you have items in your cart. Complete your purchase now!';

            // Additional information about the free gift
            $free_gift_product_id = 470; // Replace with the actual product ID of the free gift

            // Get the free gift product details
            $free_gift_product = wc_get_product($free_gift_product_id);
            
            if ($free_gift_product) {
                // Output information about the added free gift in the email
                $message .= '<p>Free Gift Added:</p>';
                $message .= '<table cellspacing="0" cellpadding="6" style="width: 100%; color: #636363; border: 1px solid #e5e5e5;" border="1"><tbody>';
                $message .= '<tr><th style="text-align: left;">Product</th><th style="text-align: left;">Quantity</th></tr>';
                $message .= '<tr>';
                $message .= '<td>' . $free_gift_product->get_name() . '</td>';
                $message .= '<td>1</td>';
                $message .= '</tr>';
                $message .= '</tbody></table>';
                
                // Send the abandoned cart email using the WordPress mail function with HTML content type
                wp_mail($user_email, $subject, $message, 'Content-Type: text/html');
            }
        }
    }
}

// Function to add a free product to the cart
function add_free_product_to_cart() {
    // Check if the free product is not already in the cart
    $free_gift_product_id = 470; // Replace with the actual product ID of the free gift
    $cart = WC()->cart->get_cart();

    $free_product_in_cart = false;
    foreach ($cart as $cart_item) {
        if ($cart_item['product_id'] == $free_gift_product_id) {
            $free_product_in_cart = true;
            break;
        }
    }

    // Add the free product to the cart if not already present
    if (!$free_product_in_cart) {
        WC()->cart->add_to_cart($free_gift_product_id);

        // Optionally, you can display a notice to the user
        wc_add_notice('Free gift has been added to your cart!', 'success');
    }
}

// Hook to clear the last cart update time and set the order placed transient when the "Thank You" page is displayed
add_action('woocommerce_thankyou', 'clear_last_cart_update_time_and_set_order_placed_transient', 10, 1);

function clear_last_cart_update_time_and_set_order_placed_transient($order_id) {
    delete_transient('last_cart_update_time');
    set_transient('order_placed_after_last_cart_update', true);
}
flexi bogo cta banner image


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

If the cart has not been updated for the specified time of 5 minutes, it is considered an abandoned cart and the notification is sent to the captured login email after the cart update time has elapsed.

How To Send Email Notification and Add a Free Gift for an Abandoned Cart? - Tyche Softwares

When the user visits the cart again, the free product will be automatically added to the cart.

How To Send Email Notification and Add a Free Gift for an Abandoned Cart? - Tyche Softwares

In case, if the order is placed the abandoned cart notifications will be stopped from being sent to the logged-in user.

Moreover, to make the emails more personal, you can also use the built-in shortcodes offered by our Abandoned Cart Pro for WooCommerce Plugin to add customers’ first names and product names to emails. This plugin gives you the essential features & options that you would like to have for abandoned cart recovery.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x