WooCommerce Cart Abandonment: How to Track and Recover Lost Carts with Custom Webhook Automations
Avoid expensive monthly recovery app fees. Learn how to capture shopping cart states and trigger automated emails using custom WooCommerce webhook actions.
For WooCommerce store owners, checkout cart abandonment is one of the biggest sources of lost revenue. While third-party plugins and email marketing services (like Klaviyo or Mailchimp) offer cart recovery integrations, they charge monthly subscription fees based on contacts or email volumes.
If you already use an internal CRM, ticket tracker, or custom database, you can build a lightweight, secure cart abandonment tracking pipeline using WooCommerce's native hooks and webhook integrations.
This guide details how to detect when a shopping session is abandoned, capture the user's cart products, and safely trigger recovery sequences without adding plugin overhead.
1. How the Cart Abandonment Pipeline Works
To track abandoned carts, your server needs to perform three tasks:
1. Listen for Activity: Monitor when a guest or logged-in user enters their email on the checkout form.
2. Save Cart State: Write the user's email, name, and cart products (items, quantities, and pricing) into a temporary database table or transient cache.
3. Set a Timeout Trigger: Schedule a task (e.g., 30 minutes after checkout inactivity) to verify if the user completed the purchase. If no completed order exists for that email, trigger the recovery webhook.
2. Capturing Checkout Emails via AJAX
To track cart abandonment, you must capture the visitor's email address as soon as they type it into the checkout email input field, rather than waiting for them to submit the form.
Add this jQuery script to your theme's frontend checkout scripts:
`javascript
jQuery(document).ready(function($) {
// Listen for email input blur on checkout page
$(document).on('blur', '#billing_email', function() {
var email = $(this).val();
if (email && email.indexOf('@') > 0) {
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
action: 'track_abandoned_email',
email: email,
billing_first_name: $('#billing_first_name').val()
}
});
}
});
});
`
3. Saving the Cart Session in WordPress
In your theme's functions.php or a helper plugin, register the AJAX action to save the email and cart details into a custom database option or transient record:
`php
add_action( 'wp_ajax_nopriv_track_abandoned_email', 'save_abandoned_cart_session' );
add_action( 'wp_ajax_track_abandoned_email', 'save_abandoned_cart_session' );
function save_abandoned_cart_session() {
$email = sanitize_email( $_POST['email'] );
if ( ! is_email( $email ) ) {
wp_send_json_error();
}
$first_name = isset( $_POST['billing_first_name'] ) ? sanitize_text_field( $_POST['billing_first_name'] ) : '';
// Retrieve active WooCommerce cart contents
$cart = WC()->cart;
if ( $cart->is_empty() ) {
wp_send_json_error();
}
$cart_data = array();
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$cart_data[] = array(
'id' => $cart_item['product_id'],
'name' => $product->get_name(),
'price' => $product->get_price(),
'quantity' => $cart_item['quantity'],
);
}
$session_payload = array(
'first_name' => $first_name,
'cart' => $cart_data,
'timestamp' => time(),
'converted' => false
);
// Save this session for 24 hours using WordPress Transients
$transient_key = 'abandoned_cart_' . md5( $email );
set_transient( $transient_key, $session_payload, DAY_IN_SECONDS );
wp_send_json_success();
}
`
4. Marking Carts as Converted on Checkout
To ensure you do not send spam recovery emails to clients who completed their purchase, you must flag the transient as "converted" as soon as an order is successfully processed:
`php
add_action( 'woocommerce_thankyou', 'flag_cart_as_converted' );
function flag_cart_as_converted( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) return;
$email = $order->get_billing_email();
$transient_key = 'abandoned_cart_' . md5( $email );
$session = get_transient( $transient_key );
if ( $session ) {
// Delete the transient because the user completed the purchase
delete_transient( $transient_key );
}
}
`
5. Dispatching the Webhook via Cron Job
Set up a scheduled cron task to run every 15 minutes. The cron script checks for saved transients that are older than 30 minutes and triggers your CRM webhook to send a recovery email.
`php
add_action( 'wc_check_abandoned_carts_cron', 'process_abandoned_cart_webhooks' );
function process_abandoned_cart_webhooks() {
global $wpdb;
// Search database options table for active abandoned cart transients
$results = $wpdb->get_results( "
SELECT option_name, option_value
FROM {$wpdb->options}
WHERE option_name LIKE '_transient_abandoned_cart_%'
" );
foreach ( $results as $row ) {
$transient_name = str_replace( '_transient_', '', $row->option_name );
$session = get_transient( $transient_name );
if ( ! $session ) continue;
// If session is older than 30 minutes and not converted
if ( ( time() - $session['timestamp'] ) > 1800 ) {
// Extract the user email hash from transient name
$email_hash = str_replace( 'abandoned_cart_', '', $transient_name );
// Dispatch webhook payload to CRM (e.g. HubSpot or Zoho Creator)
wp_remote_post( 'https://yourcrm-webhook.com/receiver', array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => json_encode( array(
'event' => 'cart_abandoned',
'first_name' => $session['first_name'],
'cart' => $session['cart'],
'email_hash' => $email_hash
)),
));
// Delete transient to prevent duplicate webhook dispatches
delete_transient( $transient_name );
}
}
}
`
By capturing billing details via AJAX, saving active cart states as transients, and scheduling a background cron job to dispatch data to your CRM, you recover lost sales automatically while eliminating recurring SaaS app overhead.
Related posts
Cross-Listing Inventory Management Without Overselling
A practical cross-listing inventory architecture covering canonical SKUs, stock states, reservations, marketplace mappings, order events, synchronization, reconciliation and recovery.
Read article →
Depop Pricing Strategy: Fees, Offers and Profit
A practical Depop pricing system covering market research, location-based fees, buyer costs, offers, discounts, bundles, shipping, boosting, returns and contribution.
Read article →
eBay Item Specifics and Product Identifiers Guide
A complete eBay structured-data guide covering categories, required and recommended item specifics, product identifiers, catalog matching, variations, condition and bulk repair.
Read article →
Author
Anushka Dahanayake
Anushka Dahanayake is the founder of ANUSHKA DAHANAYAKE (PVT) LTD, building SEO-driven content, digital services, and revenue platforms for businesses in Sri Lanka and worldwide.