How to Automatically Run Full Stock Resync at a Specific Time of the Day

What I am going to show you here is how to run the “Sync product inventory” tool from WooCommerce > Status > Tools automatically, for example, at 1 AM.

Tool which allows to run full product stock resync in the background

To do so, we need to create a scheduled action manually, I mean, programmatically, with the help of the code snippet below.

/*******************/
/*    Parameters   */
/*******************/
// for example, we want to run it at 1 AM every night since tomorrow
$timestamp = strtotime( 'tomorrow 01:00:00', current_time( 'timestamp' ) );
$store_id_or_url = 'store2.rudrastyh.com'; // provide a store URL
//$store_id_or_url = 5; // if you're using a multisite network, the store ID must be without quotes
$interval_in_seconds = DAY_IN_SECONDS; // we will do it every 24 hours (daily)

/*********************/
/*    Do the stuff   */
/*********************/
as_schedule_recurring_action( $timestamp, $interval_in_seconds, 'rudr_start_inventory_sync', array( 'store' => $store_id_or_url ) );

add_action( 'rudr_start_inventory_sync', function( $store_id_or_url ) {
	
	// in case, the plugin was deactivated, let's activate it now
	if( ! class_exists( 'Rudr_Simple_Product_Sync' ) ) {
		include_once ABSPATH . 'wp-admin/includes/plugin.php';
		activate_plugin( 'rudr-simple-inventory-sync-for-woocommerce/rudr-simple-inventory-sync-for-woocommerce.php' );
	}
	
	// you do not need to make any changes in the lines below
	$hook_name = is_numeric( $store_id_or_url ) ? 'rudr_isfw_resync_wpmu' : 'rudr_isfw_resync';
	if( false === as_next_scheduled_action( $hook_name, array( 'store' => (string) $store_id_or_url ) ) ) {
		as_schedule_recurring_action( time(), MINUTE_IN_SECONDS, $hook_name, array( 'store' => $store_id_or_url ) );
	}

} );

The parameters you need to pay attention to are:

$timestampint
It represents a UNIX timestamp when we need to run the sync.
$interval_in_secondsint
What is the recurring interval when we need to start the full resync. Provide an interval in seconds, but you can use WordPress constants, DAY_IN_SECONDS, WEEK_IN_SECONDS, etc.
$store_id_or_urlint|string
If you want to run the automatic resync for a subsite within a WordPress multisite network, then you need to provide an ID of that subsite, for example, 5. Otherwise, for standalone stores, it is a website URL in the following format: store.rudrastyh.com (without https://).

If you run the code correctly, then you can see a new scheduled action will appear in WooCommerce > Status > Scheduled Actions (the “Pending” tab).

Full stock resync scheduled actions

Need more help?