Programmatically synchronize a product across the Network
WPGlobalCart is a powerful WooCommerce plugin designed to streamline e-commerce operations within a WordPress MultiSite network. It offers a range of tools and features that simplify product management, making it an essential tool for businesses with multiple online stores.
Product Synchronization Made Easy
One of the standout features of the WPGlobalCart plugin is its user-friendly and efficient approach to product synchronization. Managing a diverse range of products across multiple websites within a WordPress MultiSite network can be a complex and time-consuming task. However, WPGlobalCart streamlines this process, offering a set of tools and features that make product synchronization a breeze.
Visual Interface for Intuitive Product Management
WPGlobalCart’s visual interface is the heart of its product synchronization capabilities. With a few clicks, you can initiate product synchronization between different websites in your network:
- Duplicate Products with Ease
- Update Products Universally
- Effortless Product Deletion
Programmatically synchronize a list of products across the Network
For developers and advanced users, the WPGlobalCart plugin offers a robust API (Application Programming Interface) that opens up a world of possibilities for custom product synchronization and integration with external systems. This API allows you to programmatically control and extend the plugin’s functionalities to suit your unique requirements.
The WPGlobalCart API exposes various methods and endpoints that can be used to interact with product data, categories, and other e-commerce elements within your WordPress MultiSite network. Let’s walk through a hypothetical scenario where you might want to programmatically synchronize product prices across your network.
include ( 'wp-load.php' ); $product_IDs = array ( 22, 24 ); $sync_data = array ( '_woogc_ps_sync_to' => array ( '3' => 'yes', '5' => 'yes' ), '_woogc_ps_maintain_child' => array ( '3' => 'yes', '5' => 'yes' ), '_woogc_ps_maintain_categories' => array ( '3' => 'no', '5' => 'yes' ), '_woogc_ps_maintain_stock' => array ( '3' => 'no', '5' => 'yes' ), 'return_interface_response' => TRUE ); global $WooGC; if ( ! isset ( $WooGC->ps_interfaces ) || ! is_object ( $WooGC->ps_interfaces ) ) { include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.php'); include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.main-product.php'); include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.child-product.php'); include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.interfaces.php'); $WooGC->ps_interfaces = new WooGC_PS_interfaces(); } if ( ! is_object ( $WooGC->ps_interfaces ) ) return; foreach ( $product_IDs as $product_ID ) { $response = $WooGC->ps_interfaces->product_save_main ( $product_ID, $sync_data ); print_r ( $response ); }
In this simplified example, we’ve created a sample code that utilizes the WPGlobalCart API to update a list of products across the network. The arguments list, takes some options along the required shop IDs. For example this block instruct the API to synchronize the provided product id to the Shops IDs 3 and 5.
'_woogc_ps_sync_to' => array ( '3' => 'yes', '5' => 'yes' )
You can customize this code to implement your specific synchronization logic and integrate with external systems or data sources.
Synchronize product(s) with the network during an import process.
When performing a product import using a third-party plugin, the synchronization process can be automated to ensure that the imported products are seamlessly replicated across designated shops within your network.
This automation can be achieved by leveraging a synchronization operation tied to the save_post action. The save_post hook is triggered whenever a new product is created or updated, making it an ideal point to initiate synchronization.
However, if the third-party plugin uses its own custom actions or hooks during the import process, you may need to adapt the synchronization logic to align with the plugin’s specific workflow. Instead of relying solely on save_post, update the synchronization code to listen for the plugin’s proprietary actions or filters. This ensures compatibility and minimizes potential conflicts or delays during the synchronization process.
For example, replace the standard save_post trigger with the custom hook provided by the third-party plugin to ensure that the synchronization operation is executed precisely when the product import completes. Properly implementing this customization ensures efficient and error-free replication of product data across your WooCommerce network.
The following code should be placed within a custom file in the /wp-content/mu-plugins/ or a custom plugin.
add_action('save_post', 'handle_new_product_creation', 10, 3); function handle_new_product_creation( $post_id, $post, $update ) { if ($post->post_type !== 'product') return; if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; //ensure not triggered through the default Save interface if ( ! empty( $_POST['woocommerce_meta_nonce'] ) ) return; if ( $post->post_status === 'publish') custom_function_on_new_product( $post_id ); } function custom_function_on_new_product( $product_id ) { $sync_data = array ( '_woogc_ps_sync_to' => array ( '3' => 'yes', '5' => 'yes' ), '_woogc_ps_maintain_child' => array ( '3' => 'yes', '5' => 'yes' ), '_woogc_ps_maintain_categories' => array ( '3' => 'no', '5' => 'yes' ), '_woogc_ps_maintain_stock' => array ( '3' => 'yes', '5' => 'yes' ), 'return_interface_response' => TRUE ); global $WooGC, $blog_id; if ( ! isset ( $WooGC->ps_interfaces ) || ! is_object ( $WooGC->ps_interfaces ) ) { include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.php'); include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.main-product.php'); include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.child-product.php'); include_once(WOOGC_PATH . '/include/product-sync/class.woogc.ps.interfaces.php'); $WooGC->ps_interfaces = new WooGC_PS_interfaces(); } if ( ! is_object ( $WooGC->ps_interfaces ) ) return; //ignroe the sync to the current shop foreach ( $sync_data as $sync_data_key => $sync_data_item ) { if ( ! is_array ( $sync_data_item ) ) continue; foreach ( $sync_data_item as $sync_data_item_key => $sync_data_item_value ) { if ( $sync_data_item_key == $blog_id ) unset ( $sync_data[ $sync_data_key ][ $sync_data_item_key ] ); } } $response = $WooGC->ps_interfaces->product_save_main ( $product_id, $sync_data ); }
By leveraging the WPGlobalCart API, you can automate complex synchronization processes, integrate with third-party services, and extend the plugin’s capabilities to meet your business needs.