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

How to Add Checkout Page Link in Abandoned Cart Reminder Emails?

Abandoned carts are recovered by sending reminder emails and to make things easier for customers you can also provide a direct link to the checkout page. This reminder along with a direct link to the checkout page makes it convenient for them to proceed to checkout, and store owners can effectively reduce the number of abandoned carts and recover potentially lost sales.

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 = 600; // 10 minutes in seconds

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

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

    // 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;
        } elseif (isset($_SESSION['user_email'])) {
            // Check if the user's email is stored in the session (for guest users)
            $user_email = $_SESSION['user_email'];
        }

        // Check if the user's email is available and WooCommerce cart is initialized
        if (!empty($user_email) && WC()->cart) {
            // 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 email message
                $message = '<p>Dear ramp,</p>';
                $message .= '<p>You have items in your cart. Complete your purchase now!</p>';
                $message .= '<p>Cart Contents:</p>';
                $message .= '<ul>';

                foreach ($cart_items as $cart_item) {
                    $product = wc_get_product($cart_item['product_id']);
                    $message .= '<li>' . $product->get_name() . ' - ' . wc_price($cart_item['data']->get_price()) . ' x ' . $cart_item['quantity'] . '</li>';
                }

                $message .= '</ul>';

                // Add the checkout URL to the email message
                $checkout_url = wc_get_checkout_url();
                $message .= '<p>Complete your purchase by clicking <a href="' . $checkout_url . '">here</a>.</p>';

                // Send the abandoned cart email using the WordPress mail function with HTML content type
                wp_mail($user_email, $subject, $message, 'Content-Type: text/html');
            }
        }
    }
}

// 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 transient once an order is placed
    delete_transient('last_cart_update_time');
}

Output

When a customer visits an online store, adds products to their cart, but leaves without completing the purchase, the code will consider it as an abandoned cart. After a certain period, let’s say 10 minutes as set in the code, if the customer hasn’t returned to complete the purchase, the system triggers an automated reminder email.
This reminder email includes a direct link to the checkout page, making it easy for the customer to proceed with the purchase with just one click.

How to Add Checkout Page Link in Abandoned Cart Reminder Emails? - Tyche Softwares

Similarly, you can recover lost sales by sending coupon codes in abandoned cart reminder emails. Check out this guide on how to send and Auto Apply Coupons in WooCommerce Abandoned Cart Reminder Emails.

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