Stock Sync uses the following actions to monitor stock changes:
woocommerce_product_set_stock
woocommerce_variation_set_stock
These actions have to be triggered in order for Stock Sync to detect stock changes and synchronize them. The hooks are automatically run by WooCommerce itself and most other 3rd party plugins. However, if you have developed a custom solution that updates stock quantities, make sure to trigger the actions appropriately.
Technically the plugin works similarly for both products and variations so you can only trigger woocommerce_product_set_stock
if you don’t know the product type.
Code that won’t work:
function update_quantity( $product, $stock_quantity ) {
update_post_meta( $product->get_id(), '_stock', $stock_quantity );
}
This code won’t work because it doesn’t trigger the action after it has changed stock quantity.
Code that works with Stock Sync:
function update_quantity( $product, $stock_quantity ) {
update_post_meta( $product->get_id(), '_stock', $stock_quantity );
do_action( 'woocommerce_product_set_stock', $product );
}
This will work because it triggers woocommerce_product_set_stock
.