Multisite Products Sync for WooCommerce

Let’s assume that you have a WordPress multisite network installed and some (or all) of the sites of the network are WooCommerce stores.

The thing is that sometimes you may need to sync WooCommerce products between the sites of your multisite network.

Because we are talking about WordPress Multisite we are not going to work with REST API, the tutorial will be all around switch_to_blog() function and CRUD layers.

If you do not want to code, you can always use my Simple Multisite Crossposting that allows to sync products between WooCommerce stores.

Creating Products

Let’s assume that we have Store 1 and Store 2 as a part of 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.

Select Multisite sites to sync WooCommerce product with.
This is how you can select specific multisite sites you would to sync a product with in case you are using my plugin.

Updating Products

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, its copy is going to be created again! And again. The question is how to prevent that?

I think it is quite a good idea to store a product ID from Site 2 in post meta of the same product on Site 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();

}
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 Twitter