Multisite Product Sync for WooCommerce

In this tutorial I am going to show you two ways how you can sync your WooCommerce products between stores within a single WordPress multisite network.

Under two ways I mean this:

  1. In code with the help of the woocommerce_update_product action hook, the switch_to_blog() function and CRUD layers (in case we’re working not with a multisite network but with standalone WooCommerce stores, then we need to use REST API, there is a separate tutorial about that).
  2. With the plugin (just install and here we go).

Below is an example of how it can be implemented on your store with my plugin:

WooCommerce multisite product sync
Just select some of the connected WooCommerce stores you would like to sync this specific product with.

There is not much to talk about when we speak in terms of the plugin approach:

So, it you want to configure WooCommerce multisite product sync without coding, please check my Simple Multisite Crossposting plugin.

The rest of the tutorial I am going to dedicate to the coding approach.

Creating WooCommerce Multisite Product Sync in Code

1. Automatically publish products on another store

Let’s assume that we have “Store 1” and “Store 2” as a part of a WordPress multisite network and when a product is created on “Store 1”, it should be automatically created on “Store 2” as well.

We all know save_post (and save_post_product) action hooks, but should we use them here? Perhaps we can look towards woocommerce_update_product appeared in WooCommerce v3 because at least you will have WC_Product object available as its second argument. There is also woocommerce_new_product hook but it is not going to be activated when you create a WooCommerce product via admin. So let’s stick to woocommerce_update_product for now.

Also my tutorial about creating products programmatically might by helpful here. For the simplicity of the code we are going to work with simple products only.

add_action( 'woocommerce_update_product', 'rudr_crosspost_product', 20, 2 );

function rudr_crosspost_product( $product_id, $product ) {

	remove_action( 'woocommerce_update_product', 'rudr_crosspost_product', 20, 2 );
	
	switch_to_blog( 2 );
	
	$new_product = new WC_Product_Simple();
	$new_product->set_name( $product->get_name() );
	$new_product->set_regular_price( floatval( $product->get_regular_price() ) );
	$new_product->save();

	restore_current_blog();

}

As you can see the code is quite simple but anyway there are a couple moments worth to keep in mind.

WooCommerce Multisite product sync between multiple stores
This is how you can select specific stores you would to sync a product with.

2. Sync product updates between WooCommerce stores

In the previous chapter I showed you how you can create WooCommerce products on multiple WooCommerce stores within a WordPress multisite network at the same time.

But what if later you decide to update the product, then its copy is going to be created again! And again. The question is how to prevent that?

How to configure WooCommerce Multisite product sync without creating duplicates

I think it is quite a good idea to store a product ID from “Store 2” in post meta of the same product on “Store 1”. And then we just have to check that metadata, if it exists, we are going to update the product, if it doesn’t – create a new one! Let’s do it!

add_action( 'woocommerce_update_product', 'rudr_crosspost_product', 20, 2 );

function rudr_crosspost_product( $product_id, $product ) {

	remove_action( 'woocommerce_update_product', 'rudr_crosspost_product', 20, 2 );

	// let's try to get ID of the same product on Site 2
	$site_2_product_id = $product->get_meta( 'site_2_product_id', true );
	// switch to Site 2
	switch_to_blog( 2 );

	if( $site_2_product_id ) {
		$site_2_product = wc_get_product( $site_2_product_id );
	}

	if( ! $site_2_product ) {
		$site_2_product = new WC_Product_Simple();
	}

	// update the info
	$site_2_product->set_name( $product->get_name() );
	$site_2_product->set_regular_price( floatval( $product->get_regular_price() ) );
	$site_2_product->save();

	restore_current_blog();

	$product->add_meta_data( 'site_2_product_id', $site_2_product->get_id(), true );
	$product->save_meta_data();

}

3. Is it better to connect products using SKU or meta field?

In the example above we used meta fields to connect synced products between stores. But the metadata itself isn’t a very persistent thing, for example if you decide to duplicate a product, then what? Two products on “Store 1” will be connected to the same product on “Store 2”. Which is of course not good!

That’s why on real projects I recommend to use SKU connection for products or at least a custom database table.

Conclusion

I created this tutorial to show you that WooCommerce multisite product sync can be easily implemented in the code and the code is not that difficult, but the main difficulty here is that you need to keep in mind so many things:

And so on. As you can see a lot of has been done with my multisite crossposting plugin, if you’re interested in how specific functionality is coded in it, just ask me in the comment.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X