Skip to content

Instantly share code, notes, and snippets.

@mymizan
Created June 22, 2020 19:46
Show Gist options
  • Save mymizan/84f3feb7570c39a58e71a357a2147c05 to your computer and use it in GitHub Desktop.
Save mymizan/84f3feb7570c39a58e71a357a2147c05 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WooMulti Demo Sync Product
* Plugin URI: https://woomultistore.com/
* Description: A demo plugin to demonstrate the synchronization of products via API.
* Author: Lykke Media AS
* Version: 1.0.0
* Author URI: https://woomultistore.com/
*/
final class WOO_MULTI_DEMO_SYNC_PRODUCT {
/**
* __construct
*
* @return void
*/
public function __construct() {
/**
* Run the code after WordPress has been initialized.
*/
add_action( 'init', array( $this, 'init' ), PHP_INT_MAX, 0 );
}
/**
* Code sync product via API
*
* @return void
*/
public function init() {
/**
* Just a quick way to hook the code.
*/
if ( ! isset( $_GET['run_product_update'] ) ) {
return;
}
/**
* These have already been synced with the child sites.
*/
$product_ids = array(
470,
468,
458,
460,
);
foreach ( $product_ids as $pid ) {
/**
* Lets make some random changes.
*/
$product = wc_get_product( $pid );
$product->set_name( uniqid() );
$product->set_description( time() );
/**
* On the version 4.1.1 the product will sync as soon as its saved.
* There's no need to fire any action.
*/
$product->save();
/**
* Version lower than that will need to fire this action.
*/
do_action( 'WOO_MSTORE_admin_product/process_product', $pid );
}
}
}
new WOO_MULTI_DEMO_SYNC_PRODUCT();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment