WooCommerce clean Orphaned variants

Find the orphaned variants

SELECT * FROM `wp_posts` o
LEFT OUTER JOIN `wp_posts` r
ON o.post_parent = r.ID
WHERE r.id IS null AND o.post_type = 'product_variation'

And delete them

DELETE o FROM `wp_posts` o
LEFT OUTER JOIN `wp_posts` r
ON o.post_parent = r.ID
WHERE r.id IS null AND o.post_type = 'product_variation'

WooCommerce incorrect out of stock error on checkout

It took me a few hours, to debug the cause of an out of stock error for a Woocommerce site.

The product variation had stock available. When performing a checkout it cause an error that the product didn't have enough stock.

I finally figured out what causes this problem:

Woocommerce tries to reserve a product via the function
'reserve_stock_for_product' (woocommerce/src/Checkout/Helpers/ReserveStock.php)

This method joins several tables: wp_wc_reserved_stock and wp_post_meta.

In my case the tables had different storage engines. wc_reserved_stock had a INNODB storage engine. wp_post_meta was MyISAM.

This fails with this particular INSERT query. Because it cannot joins these tables in a single transaction.

So don't mix table engines!!

(Which happen when you migrate an old site to a new server with a different default storage enginge)