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

How to Send and Auto Apply Coupons in WooCommerce Abandoned Cart Reminder Emails?

In WooCommerce sending the abandoned cart reminder emails is one popular technique to win back lost customers. So when the cart is abandoned we can send reminder email notifications, monitor the recovered cart from the dashboard, and export abandoned orders via a plugin such as Abandoned Cart Pro For WooCommerce. On the other hand, if you want only one of the functionality such as Auto-apply coupons out of the plugin then it is achievable with custom coding.


This code snippet will help you when a customer adds multiple items to his cart & has abandoned the cart for some reason. Here the code will be sending a reminder email with a coupon code and also with a cart URL link. Once the user clicks on the link they will be redirected to the cart page and the coupon code is automatically applied only once.

add_action('init', 'capture_abandoned_cart_email');

function capture_abandoned_cart_email() {
    // Ensure WooCommerce is active and loaded
    if (!function_exists('WC')) return;

    // Get the last cart update time from transient
    $last_cart_update_time = get_transient('last_cart_update_time');
    $threshold_time = 120; // 2 minutes in seconds

    // Get the current timestamp
    $current_time = time();

    // Calculate the time difference since the last cart update
    $time_difference = $last_cart_update_time ? $current_time - $last_cart_update_time : PHP_INT_MAX;

    // If the last cart update time is not set or the threshold time has elapsed
    if (empty($last_cart_update_time) || $time_difference >= $threshold_time) {
        // Set the last cart update time to the current time
        set_transient('last_cart_update_time', $current_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;
            $username = $current_user->display_name; // Retrieve username
            add_coupon_to_cart(); // Add the coupon code to the cart
        } elseif (isset($_SESSION['user_email'])) {
            // Check if the user's email is stored in the session (for guest users)
            $user_email = $_SESSION['user_email'];
            $username = ''; // Guest user doesn't have a username
            add_coupon_to_cart(); // Add the coupon code to the cart
        }

        // Check if the user's email is available
        if (!empty($user_email)) {
            // Get the cart contents
            $cart_items = WC()->cart->get_cart();

            // Check if the cart is not empty
            if (!empty($cart_items)) {
                // Compose the subject for the abandoned cart email
                $subject = 'Your Cart is Waiting!';

                // Compose the message for the abandoned cart email
                $message = "Dear $username, you have left some items in your cart. Grab the second chance to complete your purchase now!";

                // Add information about the coupon code and link to the cart page
                $cart_page_url = wc_get_cart_url(); // Get the cart page URL
                $message .= "\n\n" . " Don't miss out! Get your 'BOGO' coupon applied automatically in your cart now for a special discount! $cart_page_url";

                // Send the abandoned cart email using the WordPress mail function
                wp_mail($user_email, $subject, $message);
            }
        }
    }
}

// Function to add a coupon code to the cart
function add_coupon_to_cart() {
    // Check if WooCommerce is active and the cart is available
    if (class_exists('WooCommerce') && WC()->cart) {
        // Add the coupon code to the cart
        $coupon_code = "BOGO"; // Coupon code to be added to the cart
        WC()->cart->apply_coupon($coupon_code);
    }
}

// Hook to delete transient after order is placed
add_action('woocommerce_thankyou', 'delete_abandoned_cart_transient');

function delete_abandoned_cart_transient($order_id) {
    // Delete the transients once an order is placed
    delete_transient('last_cart_update_time');
}

Output

If a customer visits the cart page, adds items to their cart, and abandons the cart, the abandoned cart reminder email will be sent to the customer after the cutoff time of 2 minutes as set in the code. Once the 2-minute cutoff time of the last cart update time has passed without any activity, the email with the cart link will be sent.

How to Send and Auto Apply Coupons in WooCommerce Abandoned Cart Reminder Emails? - Tyche Softwares

When the customer clicks on the cart link provided in the email, it will redirect them to their cart page. Upon redirection, the ‘BOGO’ coupon will already be applied to their cart, providing them with a special discount to complete their purchase.

How to Send and Auto Apply Coupons in WooCommerce Abandoned Cart Reminder Emails? - Tyche Softwares

The email reminder sent for abandoned carts looks simple right? Yes, alternatively, looking for more personalized Email templates. You can try out using our free Abandoned Cart lite plugin for WooCommerce which helps you to track and recover abandoned orders easily.


Browse more in: Abandoned Carts, 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