Manually trigger an order synchronization operation
The WooCommerce Global Cart plugin streamlines order management by implementing a Global Order functionality, ensuring that orders are seamlessly physically synchronized across designated shops within your network. With synchronization occurring both during order creation and updates, businesses can maintain consistency and reduce manual workload.
In cases where an order created prior to enabling the synchronization feature needs to be migrated to a different shop, a manual trigger through custom code offers a reliable solution. This targeted approach allows for flexibility and precision in managing legacy orders while harnessing the powerful network-wide capabilities of the Global Cart system.
The following code example should be placed within a custom file on the wordpress root.
The $woogc_consumer_key, $woogc_consumer_secret, $woogc_api_url, $woogc_sync_to_blog_id, $woogc_api_url, $woogc_sync_to_blog_id variables should be changed accordingly to the network set-up.
Also the woogc_trigger_order_sync ( 324 ) should be updated with the order ID required to synchronize.
include_once( 'wp-load.php' ); // WooCommerce API credentials global $woogc_consumer_key, $woogc_consumer_secret, $woogc_api_url, $woogc_sync_to_blog_id; $woogc_consumer_key = 'ck_3b0643eb1c6e814eac68d026389fda9c738dbdb2'; $woogc_consumer_secret = 'cs_6964e0d1f3226f240d98395ba8bd38bf0022d96c'; // WooCommerce API URL $woogc_api_url = 'http://woos-sc2.ldev/wp-json/custom-api/v1/create-order'; $woogc_sync_to_blog_id = 4; woogc_trigger_order_sync ( 324 ); function woogc_trigger_order_sync( $order_id ) { global $blog_id; $order = new WC_Order ( $order_id ); $order_data = array(); $order_data = $order->get_data(); $unsets = array( 'id', 'parent_id', 'number', 'total_tax', 'meta_data', 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); foreach ( $unsets as $unset ) unset ( $order_data[ $unset ] ); foreach ( $order->get_items() as $order_item_id => $order_item_data ) { $order_data["line_items"][] = array ( "product_id" => $order_item_data->get_product_id(), "variation_id" => $order_item_data->get_variation_id(), "quantity" => $order_item_data->get_quantity(), "subtotal" => $order_item_data->get_subtotal(), "subtotal_tax" => $order_item_data->get_subtotal_tax(), "total" => $order_item_data->get_total(), "total_tax" => $order_item_data->get_total_tax(), "blog_id" => $order_item_data->get_meta( 'blog_id' ), ); } foreach ( $order->get_items( 'shipping' ) as $order_item_id => $order_item_data ) { $order_data["shipping_lines"][] = array ( "method_title" => $order_item_data->get_name(), "total" => $order_item_data->get_total() ); } //add meta data $order_data['meta_data'] = array ( '_woogc_origin_shop' => $blog_id, '_woogc_origin_order_id' => $order->get_ID(), ); $operation_type = 'new_order'; $woogc_synchronized_to = woogc_submit_to_api( $order_data, $operation_type ); $order->update_meta_data( '_woogc_synchronized_to', $woogc_synchronized_to ); //$order->save(); } function woogc_submit_to_api( $order_data, $operation_type ) { global $woogc_consumer_key, $woogc_consumer_secret, $woogc_api_url, $woogc_sync_to_blog_id; // Combine the consumer key and secret $auth = base64_encode( $woogc_consumer_key . ':' . $woogc_consumer_secret ); // Set up the request headers $headers = array( 'Authorization' => 'Basic ' . $auth, 'Content-Type' => 'application/json' ); // Prepare the data to send (empty object as example) $body = json_encode( $order_data ); $woogc_synchronized_to = array(); // Send the request $response = wp_remote_post( $woogc_api_url, array( 'method' => 'POST', 'body' => $body, 'headers' => $headers, 'timeout' => 10, 'sslverify' => false )); // Check for success or failure if ( is_wp_error($response) ) error_log('Error: ' . $response->get_error_message()); else { $response_body = (array)json_decode( wp_remote_retrieve_body( $response ) ); if ( ! is_array ( $response_body ) || ! isset ( $response_body['order_id'] ) ) return; $woogc_synchronized_to[] = $woogc_sync_to_blog_id; } return $woogc_synchronized_to; }