Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shameemreza/2969ffcfb013a255917ed6c39d502f53 to your computer and use it in GitHub Desktop.

Select an option

Save shameemreza/2969ffcfb013a255917ed6c39d502f53 to your computer and use it in GitHub Desktop.
Manually resets and regenerates the WooCommerce customer lookup table. Add to your theme or as a temporary plugin, then visit any admin page to trigger the fix. Safe to remove afterward.
<?php
/**
* Fix WooCommerce Customer Data Display
*
* Add this code to your theme's functions.php file or as a small custom plugin.
* After adding, visit any page in your WordPress admin to trigger the fix.
* Once the tables are regenerated, you can safely remove this code.
*/
// Only run in admin
add_action('admin_init', 'fix_woocommerce_customer_lookup_tables');
function fix_woocommerce_customer_lookup_tables() {
// Only run once - set a transient to prevent multiple runs
if (get_transient('wc_customer_lookup_tables_reset_running')) {
return;
}
// Set transient to prevent multiple runs (expires after 10 minutes)
set_transient('wc_customer_lookup_tables_reset_running', true, 10 * MINUTE_IN_SECONDS);
global $wpdb;
// 1. Truncate the customer lookup table
$wpdb->query("TRUNCATE TABLE {$wpdb->prefix}wc_customer_lookup");
// 2. Schedule regeneration of customer data
if (class_exists('\Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore')) {
// Queue regeneration for each customer
$customer_roles = apply_filters('woocommerce_analytics_import_customer_roles', array('customer'));
$user_query = new WP_User_Query(array(
'fields' => 'ID',
'role__in' => $customer_roles,
));
foreach ($user_query->get_results() as $user_id) {
\Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::update_registered_customer($user_id);
}
}
// 3. Clear any related caches
if (function_exists('wc_cache_helper') && method_exists('WC_Cache_Helper', 'get_transient_version')) {
WC_Cache_Helper::get_transient_version('customers', true);
}
// Set a flag to indicate completion
update_option('wc_customer_lookup_tables_reset_completed', current_time('mysql'));
// Delete the transient to allow running again if needed
delete_transient('wc_customer_lookup_tables_reset_running');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment