Orphaned order data after restoring WooCommerce SQL dump to HPOS-enabled site
0 reputation · 17 Jul 2024, 22:49 UTC
0 reputation · 17 Jul 2024, 22:49 UTC
After importing a WooCommerce database dump into a live store that has HPOS enabled, administrators notice that order‑related tables may contain rows that do not correspond to each other—for example, order items without matching orders or stats entries missing from the HPOS set. The goal is to confirm whether the restored dataset is internally consistent without relying on manual SQL checks. Because the raw dump does not store the HPOS migration flag and WooCommerce’s built‑in reconciliation tool only reports mismatches, it is unclear what level of verification can be automated or which tables must be compared to guarantee integrity.
Which combination of legacy and HPOS tables should be checked to detect orphaned order items, missing operational data, or out‑of‑sync stats after a restore? Can the order‑reconciliation endpoint be extended to automatically flag inconsistencies that would break admin lists or reports, or must administrators rely on custom queries? Is there a reliable way to preserve or re‑apply the HPOS migration status during the import process to avoid split‑storage scenarios?
After importing a legacy WooCommerce dump into a site that has High‑Performance Order Storage (HPOS) enabled, the main risk is that some order‑related rows remain in the legacy tables while the HPOS tables are partially populated. This creates orphaned order items, missing stats, and mismatched operational data that can break admin lists and reports.
wp_posts with post_type = 'shop_order' and moves items to wp_woocommerce_order_items and meta to wp_woocommerce_order_itemmeta.wp_woocommerce_order_stats and reference the same order ID.wp_options under woocommerce_use_hpos (value 1 when enabled).Check the migration flag
SELECT option_value FROM wp_options WHERE option_name='woocommerce_use_hpos';
It must return 1. If it is 0 or missing, the site will treat orders as legacy and you will see split storage.
Validate order‑to‑item linkage
SELECT COUNT(*) FROM wp_woocommerce_order_items WHERE order_id NOT IN (
SELECT ID FROM wp_posts WHERE post_type='shop_order'
);
Result should be 0. Any non‑zero count indicates orphaned items.
Validate item meta linkage
SELECT COUNT(*) FROM wp_woocommerce_order_itemmeta WHERE order_item_id NOT IN (
SELECT order_item_id FROM wp_woocommerce_order_items
);
Result should be 0. Non‑zero values mean orphaned meta rows.
Validate stats linkage
SELECT COUNT(*) FROM wp_woocommerce_order_stats WHERE order_id NOT IN (
SELECT ID FROM wp_posts WHERE post_type='shop_order'
);
Result should be 0. Any non‑zero count indicates missing statistical records.
Run the built‑in reconciliation endpoint
curl -X GET https://example.com/wp-json/wc/v3/reconcile/orders
The JSON response contains a mismatches array. It should be empty; otherwise review the listed items.
Optional: Cross‑check legacy meta
SELECT COUNT(*) FROM wp_postmeta WHERE meta_key LIKE 'order_id' AND meta_value NOT IN (
SELECT ID FROM wp_posts WHERE post_type='shop_order'
);
Non‑zero indicates legacy meta pointing to missing orders.
The current endpoint only returns a list of mismatches; it does not auto‑flag them in the admin UI. Extending it would require custom plugin code that hooks into the REST API response and writes back a flag or triggers a scheduled task. Until such a plugin exists, administrators should rely on the SQL checks above or write a custom query to surface the same data in the admin area.
When restoring the dump, ensure the woocommerce_use_hpos option is set to 1 in wp_options. If the dump omitted this row, manually insert it:
INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('woocommerce_use_hpos', '1', 'yes');
Alternatively, use a SQL script that first checks for existence and updates or inserts accordingly:
REPLACE INTO wp_options (option_name, option_value, autoload) VALUES ('woocommerce_use_hpos', '1', 'yes');
Doing this prevents the site from falling back to legacy storage and avoids split‑storage scenarios.
To guarantee internal consistency after a restore, run the five SQL checks above, confirm the migration flag, and verify the reconciliation endpoint reports no mismatches. If any of these checks fail, address the orphaned rows before relying on admin reports.
Do you know whether the target database contains the wp_woocommerce_order_stats table? (It exists from WooCommerce 8.0 onward.) If it is absent, the stats check is not applicable.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.