I want sync stock quantity of site B (remote site) with site A, ie. when an order in site A is in process status, the quantity of same product in site B will be updated.
For mapping product IDs, I add post meta in both site A and B.
In order to do so, I use following codes. It works, but my problem is speed process when stock quantity wants to be updated in site B, it takes more seconds.
add_action('woocommerce_order_status_changed', 'update_base_site_inventory',11, 1);
function update_base_site_inventory($order_id){
$order = wc_get_order( $order_id );
if ($order->status == 'processing') {
$items = $order->get_items();
foreach($items as $item){
$product = wc_get_product($item->get_product_id());
$product_quantity = $product->get_stock_quantity();
$id_mapped = get_post_meta($item->get_product_id(), 'map_product_id', true);
$batch_update['update'] = array(array('id' => current($id_mapped), 'stock_quantity' => $product_quantity));
}
$api_response = wp_remote_request(
'https://...../wp-json/wc/v3/products/batch/', array(
'method' => 'POST',
'headers' => array('Authorization' => 'Basic ' . base64_encode( '....:.....' )),
'body' => $batch_update
)
);
}
}
is my approach correct? what should I do? is there any way to increase the API request speed? Thanks,