WooCommerce – Fix Missing WooCommerce Order Stats – wp_wc_order_stats
Problem: Upgrading to HPOS (High Performance Order Storage) requires syncing files from standard WordPress wp_postmeta and wp_posts tables to HPOS tables, including the wp_wc_order_stats table. And not all stats synced.
Solution: ChatGPT code that adds a click to Fix Now button at the top of WordPress Admin pages. Detects and regenerates missing records in wp_wc_order_stats for HPOS compatibility. Triggers the woocommerce_order_object_updated_props action manually, which WooCommerce uses internally to regenerate stats when order properties are changed.
Source: ChatGPT, version “Working Fix Plugin Code (No Deprecated/Removed Methods)” (gotta scroll to it)
Code is saved in file called fix-missing-order-stats.php inside a folder called fix-missing-order-stats then uploaded to wp-content/plugins/ and activated in Plugins. Fix Now button is available at the top of all WordPress admin pages.
<?php
/**
* Plugin Name: Fix Missing WooCommerce Order Stats
* Description: Detects and regenerates missing records in wp_wc_order_stats for HPOS compatibility.
* Version: 2.1.0
* Author: Your Name
*/
// THIS VERSION WORKS!
// ChatGPT: Working Fix Plugin Code (No Deprecated/Removed Methods)
// https://chatgpt.com/c/681e74dc-995c-800c-af49-c7990868ab1b
// triggers the woocommerce_order_object_updated_props action manually, which WooCommerce uses internally to regenerate stats when order properties are changed.
if (!defined('ABSPATH')) exit;
// Hook into admin_init to run processing
add_action('admin_init', 'ffos_maybe_process');
function ffos_maybe_process() {
if (!is_admin() || !current_user_can('manage_woocommerce')) return;
if (!isset($_GET['ffos_run']) || $_GET['ffos_run'] !== '1') return;
if (!class_exists('WooCommerce') || !function_exists('wc_get_order')) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p><strong>Error:</strong> WooCommerce is not properly initialized. Please ensure it is active.</p></div>';
});
return;
}
global $wpdb;
$missing_order_ids = $wpdb->get_col("
SELECT o.id
FROM {$wpdb->prefix}wc_orders o
LEFT JOIN {$wpdb->prefix}wc_order_stats s ON o.id = s.order_id
JOIN {$wpdb->prefix}posts p ON p.ID = o.id
WHERE s.order_id IS NULL
AND p.post_type = 'shop_order'
ORDER BY o.id ASC
LIMIT 100
");
if (empty($missing_order_ids)) {
add_action('admin_notices', function () {
echo '<div class="notice notice-success"><p>✅ No missing order stats found.</p></div>';
});
return;
}
foreach ($missing_order_ids as $order_id) {
$order = wc_get_order($order_id);
if ($order && is_callable([$order, 'save'])) {
// Trigger WC internals to regenerate order stats
$order->set_date_modified(time()); // Minor change to trigger update
$order->save();
}
}
$count = count($missing_order_ids);
$ids = implode(', ', $missing_order_ids);
add_action('admin_notices', function () use ($count, $ids) {
echo '<div class="notice notice-success"><p>';
echo "✅ Processed {$count} orders:<br><code>{$ids}</code>";
echo '</p></div>';
});
}
// Add Fix Now button
add_action('admin_notices', 'ffos_admin_button_notice');
function ffos_admin_button_notice() {
if (!current_user_can('manage_woocommerce')) return;
$url = admin_url('?ffos_run=1');
echo '<div class="notice notice-info">';
echo '<p><strong>WooCommerce:</strong> Some orders may be missing from <code>wp_wc_order_stats</code>. ';
echo "<a href=\"{$url}\" class=\"button-primary\">Fix Now</a></p>";
echo '</div>';
}


