Home Arrow Icon Knowledge base Arrow Icon WordPress Code Related Arrow Icon Woocommerce Multisite - Dropshipping Code Samples
    Woocommerce multisite wordpress woocommerce code samples

Woocommerce Multisite - Dropshipping Code Samples

Sync Product Stock Quantities Across Sites

Keeping stock quantities synchronized across multiple sites is crucial for managing inventory effectively.

Code to Sync Stock Quantities

Add this code to your theme’s functions.php file or a custom plugin:

php
function sync_stock_quantity_to_subsites($product_id) { $main_product = wc_get_product($product_id); if (!$main_product) { return; } $stock_quantity = $main_product->get_stock_quantity(); // Loop through subsites $sites = get_sites(); foreach ($sites as $site) { if ($site->blog_id != 1) { // Skip the main site switch_to_blog($site->blog_id); // Get the corresponding product in the subsite $sub_product = wc_get_product_by_sku($main_product->get_sku()); if ($sub_product) { $sub_product->set_stock_quantity($stock_quantity); $sub_product->save(); } restore_current_blog(); } } } add_action('woocommerce_product_set_stock', 'sync_stock_quantity_to_subsites');

This function synchronizes the stock quantity from the main site to all subsites whenever the stock quantity is updated.

Add a Custom Product Meta and Sync It

Sometimes, you may need to add custom metadata to products and synchronize it across sites.

Code to Add and Sync Custom Product Meta

Add this code to your theme’s functions.php file or a custom plugin:

php
// Add a custom field to the product edit page add_action('woocommerce_product_options_general_product_data', 'add_custom_product_field'); function add_custom_product_field() { woocommerce_wp_text_input(array( 'id' => '_custom_product_field', 'label' => __('Custom Product Field', 'woocommerce'), 'desc_tip' => 'true', 'description' => __('Enter a custom value for this product.', 'woocommerce'), )); } // Save the custom field value add_action('woocommerce_process_product_meta', 'save_custom_product_field'); function save_custom_product_field($post_id) { $custom_field_value = isset($_POST['_custom_product_field']) ? sanitize_text_field($_POST['_custom_product_field']) : ''; update_post_meta($post_id, '_custom_product_field', $custom_field_value); } // Sync the custom field value to subsites function sync_custom_field_to_subsites($product_id) { $main_product = wc_get_product($product_id); if (!$main_product) { return; } $custom_field_value = get_post_meta($product_id, '_custom_product_field', true); // Loop through subsites $sites = get_sites(); foreach ($sites as $site) { if ($site->blog_id != 1) { // Skip the main site switch_to_blog($site->blog_id); // Get the corresponding product in the subsite $sub_product = wc_get_product_by_sku($main_product->get_sku()); if ($sub_product) { update_post_meta($sub_product->get_id(), '_custom_product_field', $custom_field_value); } restore_current_blog(); } } } add_action('woocommerce_update_product', 'sync_custom_field_to_subsites');

This code adds a custom field to the product edit page in the WooCommerce admin, saves the custom field value, and synchronizes it across all subsites whenever the product is updated.

Sync Order Status Back to Main Site

If you want to update the order status from a subsite back to the main site, you can use the following code:

Code to Sync Order Status

Add this code to your theme’s functions.php file or a custom plugin on the subsites:

php
function sync_order_status_to_main_site($order_id) { $order = wc_get_order($order_id); if (!$order) { return; } $order_status = $order->get_status(); // Switch to the main site switch_to_blog(1); // Assuming the main site is blog ID 1 // Find the corresponding order on the main site $main_order_id = get_post_meta($order_id, '_main_site_order_id', true); if ($main_order_id) { $main_order = wc_get_order($main_order_id); if ($main_order) { $main_order->update_status($order_status); } } restore_current_blog(); } add_action('woocommerce_order_status_changed', 'sync_order_status_to_main_site');

This function updates the order status on the main site whenever the status changes on a subsite. You need to ensure that _main_site_order_id meta key is set when the order is initially created on the main site.

Automatically Set Main Site Order ID in Subsite Orders

To ensure that each subsite order has a reference to the main site order, set the _main_site_order_id when the order is created.

Code to Set Main Site Order ID

Add this code to your theme’s functions.php file or a custom plugin:

php
function set_main_site_order_id($order_id) { $order = wc_get_order($order_id); if (!$order) { return; } // Switch to the main site to create a corresponding order switch_to_blog(1); // Assuming the main site is blog ID 1 // Create a new order in the main site $main_order = wc_create_order(); $main_order->set_status($order->get_status()); $main_order->set_total($order->get_total()); $main_order->set_address($order->get_billing_address(), 'billing'); $main_order->set_address($order->get_shipping_address(), 'shipping'); $main_order->save(); // Get the main site order ID $main_order_id = $main_order->get_id(); restore_current_blog(); // Save the main site order ID to the subsite order update_post_meta($order_id, '_main_site_order_id', $main_order_id); } add_action('woocommerce_checkout_order_processed', 'set_main_site_order_id');

This function creates a corresponding order on the main site when an order is placed on a subsite and saves the main site order ID to the subsite order.

These additional code samples provide more functionalities for synchronizing products, custom metadata, stock quantities, and order statuses across a WooCommerce multisite network. Depending on your specific needs, you can extend or modify these examples to fit your requirements. Always test thoroughly in a staging environment before deploying to a live site.