Stock Sync – How to sync products in a WooCommerce Multisite

There are a lot of reasons to use the Multisite feature on a WordPress environment – the challenge is when we need to synchronize information between them. Same database, but different tables, languages and product stocks are great examples of common data between all websites. Let’s start from the beginning.

What is WordPress Multisite?

Using the same hosting, network and dashboard, WordPress allow us to create several websites in the same place. The advantages:

  • Same plugins/version on all websites (less maintenance time);
  • Same code, different content;
  • Same themes across the websites;
  • Better content and network management;
  • Allow us to divide the websites using subdirectories (e.g. samuelsilva.pt/courses) or subdomains (e.g. courses.samuelsilva.pt);
  • Multisite means less code, literally.

How do the same products sync on all websites?

They don’t sync – there is no communication between websites. Each website works with different tables in the database – sometimes can be redundant, but remember – different content is the main advantage of a WP multisite environment.

How to sync the products on the same multisite network?

That’s why you’re here. There are many ways to sync the products, so I’ll suggest to you which I consider the best one (without plugins).

Before anything, please check the most important WordPress functions we will use on this code snippet:

  • get_sites() – retrieves the list of the websites from our network, depending of the args we use. In this specific case, we will use the default args;
  • switch_to_blog() – sending the blog_id (site ID) as a parameter, all your further queries results will come from that specific blog;
  • restore_current_blog() – come back the queries to the main website.

Using these functions, we will be able to create a bridge between all the websites in the multisite network. We have a lot of possibilities, but let’s focus on a specific need – synchronize stock units between websites.

Important question: Which field will be our key (foreign key for SQL people)?

I don’t recommend we use the most obvious – product_id. Remember, there isn’t any relationship between the websites, so we can’t trust an autoincremented field. Let’s use the product SKU in our example. Let’s go forward to our snippet:

Stock Products between websites – code snippet

Follow we can find my suggestion on a code snippet. If you have any questions or suggestions, please write a comment on this post.

/* 
* Sync products stocks by the lowest. Run this function for each product
* @param	string	$sku	Product SKU
*/
public function sync_stock_by_sku ( $sku ){

	// Firstly, check if we're on a WP multisite environment
	if( ! function_exists( 'get_sites' ) || $sites = get_sites() ){
		return;
	}
	
	$stores = array();

	// Prepare and array with the sites Ids
	foreach ( $this->sites as $site ) {

		// Organize the multidimensional array, indexing by country code
		$country_c 			  = str_replace( '/', '', $site->path );	
		$stores[ $country_c ] = $site->blog_id;

	}

	// in this case, we will sync all the products to the lowest stock store for this specific product
	$lowest_stock = false;

	$product_id = wc_get_product_id_by_sku( $sku );
	$product    = wc_get_product( $product_id );

	// Websites loop
	foreach( $stores as $c_code => $store_id ){

		// prepare the queries to run for each website
		switch_to_blog( $store_id );

		

		
		// Continue the loop if the line item is empty, for example
		if( ! $product ){
			continue;
		}
		
		// If this is the first product, override the lowest stock entry
		// and continue the loop
		if ( ! $lowest_stock && is_bool( $lowest_stock ) ) {
			$lowest_stock = $product->get_stock_quantity();
			continue;
		}
	
		// Check if the stock from this store is lower than the current one
		$stock = $product->get_stock_quantity();

		if ( $lowest_stock > $stock ){
			$lowest_stock = $stock;
		}


	}

	// Now we have the lowest stock value. Let's reloop and sync stock quantity to the lowest value

	foreach( $stores as $c_code => $store_id ){

		// prepare the queries to run for each website
		switch_to_blog( $store_id );

		$product_id = wc_get_product_id_by_sku( $sku );
		$product    = wc_get_product( $product_id );


		// Update the stock quantity
		wc_update_product_stock( $product, $lowest_stock );
	}

	// Restore queries to the current site
	restore_current_blog();

	// Return the new stock
	return $lowest_stock;

}

Leave a comment

Your email address will not be published. Required fields are marked *