phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
568 stars 211 forks source link

Updating Inventory currently is inefficient #253

Open tronics opened 2 years ago

tronics commented 2 years ago

There seems to be a limitation with the current location based inventory management. Up to API version 2010-10 there is no efficient way (eg.:GraphQL) to update inventory for multiple variations/products available to my research.

I cannot seem to wrap my head around that the available stock has to be processed individually for each variant.

My 157 products have 4000 variations. This results in 4000 API requests, takes 25 minutes and even introduces the risk of getting throttled even more. A solution that would allow execution within 2 minutes would be acceptable.

Working code:

$arrAllStock=array(); //filled from mysql or other source
$arrAllStock[] =  array (
        "location_id" => '62241177806',
        "inventory_item_id" => '42730047963342',
        "available" => 33
            );
$arrResultarray=array();

foreach($arrAllStock as $arrStock ) {
try {
       $result = $shopify->InventoryLevel->set($arrStock);
       $arrResultarray['success'][]=$arrStock;
    }catch (exception $e) { //if products have changed they could fail.
       print $e->getMessage();
       $arrResultarray['error'][]=$arrStock;
    }
}
print_r($arrResultarray);

The better alternative would be to be able to hand over the complete array and let that process more efficiently. $result = $shopify->InventoryLevel->set($arrAllStock);

jassMarok commented 2 years ago

In a similar situation although, I am reducing no. of updates I need to make by comparing total quantity from product list call to sum of location inventory coming from external system and only making a change if it's not the same.

tronics commented 2 years ago

I do something similar. I have a mysql database where I compare what has changed. And to speed up the process I update those first that are low on stock. Currently I try to find out the graphql variation of this to be able to update around 50 products at a time.