How speed up the woocommerce REST API in sync product stock quantity?

1.1k Views Asked by At

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,

1

There are 1 best solutions below

0
On

Try using WooCommerce Client Library, install using composer:

composer require automattic/woocommerce

Then use it in your code like this:

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') {
        $data = array();
        $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);
            $data[] = array(
                'id' => current($id_mapped),
                'stock_quantity' => $product_quantity
            );
        }

        // Include the library
        require __DIR__ . '/vendor/autoload.php';
        // Initialize the library
        $woocommerce = new Automattic\WooCommerce\Client(
            'https://example.com', // Your store URL
            'ck_....................', // Your consumer key
            'cs_....................', // Your consumer secret
            [
                'wp_api' => true, // Enable the WP REST API integration
                'version' => 'wc/v3', // WooCommerce WP REST API version
                'verify_ssl' => false
            ]
        );
        // Make the update
        $woocommerce->post('products/batch', array(
            'update' => array($data),

        ));
    }
}