Abandoned carts are a common issue in e-commerce, leading to lost sales and revenue. By recording abandoned carts in WooCommerce, you can analyze why customers leave their carts and develop strategies to recover those sales. This guide will show you how to implement a system to record abandoned carts in WooCommerce without using a plugin.
Step 1: Create a Custom Database Table
First, we need to create a custom database table to store the abandoned cart data. Add the following code to your theme’s functions.php file.
/*
* Snippet: How to Record Abandoned Carts in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1282
* Tested with WooCommerce 10.7.0
* "Create custom database table for abandoned carts"
*/
function wcsuccess_create_abandoned_cart_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'abandoned_carts';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
cart_contents longtext NOT NULL,
cart_total decimal(10,2) NOT NULL,
date_abandoned datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
add_action('after_switch_theme', 'wcsuccess_create_abandoned_cart_table');
This code creates a custom database table named wp_abandoned_carts to store the cart data, user ID, cart total, and the date the cart was abandoned.
Step 2: Record Abandoned Carts
Next, we need to detect when a cart is abandoned and save the cart data to our custom table. Add the following code to your functions.php file.
/*
* Snippet: How to Record Abandoned Carts in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1282
* Tested with WooCommerce 10.7.0
* "Detect and record abandoned carts"
*/
function wcsuccess_record_abandoned_cart() {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$cart_contents = WC()->cart->get_cart();
$cart_total = WC()->cart->get_total('edit');
$cart_data = array();
foreach ($cart_contents as $cart_item_key => $cart_item) {
$cart_data[] = array(
'product_id' => $cart_item['product_id'],
'quantity' => $cart_item['quantity']
);
}
global $wpdb;
$table_name = $wpdb->prefix . 'abandoned_carts';
$wpdb->insert(
$table_name,
array(
'user_id' => $user_id,
'cart_contents' => maybe_serialize($cart_data),
'cart_total' => $cart_total,
'date_abandoned' => current_time('mysql')
)
);
}
}
add_action('wp_logout', 'wcsuccess_record_abandoned_cart');
add_action('woocommerce_cart_updated', 'wcsuccess_record_abandoned_cart');
This code hooks into the wp_logout and woocommerce_cart_updated actions to record the cart data when a user logs out or the cart is updated.
Step 3: Display Abandoned Carts in the Admin Area
To view the abandoned carts, we’ll create a custom admin page. Add the following code to your functions.php file.
/*
* Snippet: How to Record Abandoned Carts in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1282
* Tested with WooCommerce 10.7.0
* "Create admin page for viewing abandoned carts"
*/
function wcsuccess_add_abandoned_cart_menu() {
add_menu_page(
'Abandoned Carts',
'Abandoned Carts',
'manage_options',
'abandoned-carts',
'wcsuccess_display_abandoned_carts',
'dashicons-cart',
26
);
}
add_action('admin_menu', 'wcsuccess_add_abandoned_cart_menu');
function wcsuccess_display_abandoned_carts() {
global $wpdb;
$table_name = $wpdb->prefix . 'abandoned_carts';
$abandoned_carts = $wpdb->get_results("SELECT * FROM $table_name");
echo '<div class="wrap">';
echo '<h1>Abandoned Carts</h1>';
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr><th>ID</th><th>User ID</th><th>Cart Contents</th><th>Cart Total</th><th>Date Abandoned</th></tr></thead>';
echo '<tbody>';
foreach ($abandoned_carts as $cart) {
$cart_contents = maybe_unserialize($cart->cart_contents);
echo '<tr>';
echo '<td>' . $cart->id . '</td>';
echo '<td>' . $cart->user_id . '</td>';
echo '<td><pre>' . print_r($cart_contents, true) . '</pre></td>';
echo '<td>' . $cart->cart_total . '</td>';
echo '<td>' . $cart->date_abandoned . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
echo '</div>';
}
This code creates an admin page under the “Abandoned Carts” menu where you can view the abandoned carts data.
Step 4: Secure the Admin Page
To ensure only users with the appropriate permissions can access the abandoned carts page, we need to add a capability check. Update the wcsuccess_add_abandoned_cart_menu function as follows:
/*
* Snippet: How to Record Abandoned Carts in WooCommerce Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1282
* Tested with WooCommerce 10.7.0
* "Secure the admin page"
*/
function wcsuccess_add_abandoned_cart_menu() {
add_menu_page(
'Abandoned Carts',
'Abandoned Carts',
'manage_woocommerce',
'abandoned-carts',
'wcsuccess_display_abandoned_carts',
'dashicons-cart',
26
);
}
add_action('admin_menu', 'wcsuccess_add_abandoned_cart_menu');
This change restricts access to users with the manage_woocommerce capability, which is typically assigned to shop managers and administrators.
Conclusion
By following the steps outlined in this guide, you can implement a system to record abandoned carts in WooCommerce without using a plugin. This allows you to track and analyze abandoned carts, helping you identify potential issues and recover lost sales.
Recording abandoned carts gives you valuable insights into your customers’ shopping behavior and enables you to take targeted actions to improve your store’s performance. Implement this feature on your WooCommerce store and start addressing abandoned cart issues effectively.
Place these code snippets in your theme’s functions.php file to implement the abandoned cart recording system. Customize the provided code to fit your specific needs and enhance your WooCommerce store’s functionality.
With this setup, you’ll be able to monitor abandoned carts and take action to recover lost sales, leading to increased revenue and a better customer experience. Happy coding!
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

