Update inventory for 4000 variations from ERP every 5 min using REST or GraphQL

Hello,

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.

Other shop solutions that I use allow me to do that with 157 queries that take not more then 4 seconds in total.

The following is using https://shopify.dev/api/admin-rest/2021-10/resources/inventorylevel#[post]/admin/api/2021-10/inventory_levels/set.json

Working code using a REST API wrapper from https://github.com/phpclassic/php-shopify.

$arrAllStock[] =  array (
		"location_id" => '62241177806',
		"inventory_item_id" => '42730047963342',
		"available" => 33
			);

foreach($arrAllStock as $arrStock ) {
    $result = $shopify->InventoryLevel->set($arrStock);
}

//Better solution would be just 1 call

$result = $shopify->InventoryLevel->set($arrAllStock);

Handing over the complete array to do just 1 REST call instead of 4000 would be much better.

This problem is not an individual problem but applies to many shops out there. It is strange this is not covered.

I have seen GraphQL that adjusts available stock. But this fails for me, seemingly because the locationid has to be connected with the inventoryid with the script above first.

1 Like

Is there a better place to find a solution for updating stock?

We use this GraphQL mutation

https://shopify.dev/api/admin-graphql/2021-10/mutations/inventorybulkadjustquantityatlocation

It is quite efficient . You have to manage the handle the potential throttle and stay under it as you batch multiple adjustments in each GraphQL call.

2 Likes

That is a great and much appreciated solution. I will mark it as solution later!

It would be even better to be able to write values directly and not deltas.
I would argue deltas will need more checking with the source and current stock.

Example in a ERP database (that also sells to other marketplaces or shops):

  1. ERP had 10 shirts on last sync

  2. Shopify sold 2 shirts

  3. ERP also sold 1 shirt meanwhile

So to get the delta:

  1. GET current Shopify stock

  2. GET current ERP stock

  3. Compare and calculate delta (how many were sold, and what delta is that to the current stock)

  4. Push delta

That is quite a task.
I imagine a simpler solution in getting what was sold and substracting from ERP current and pushing that direct value.
This is seemingly only possible in the REST API.

Thank you!

I tried a lot and I used the same example in the documentation could you please give me a example cuz I just receive bad request
https://community.shopify.com/c/shopify-apis-and-sdks/graphql-inventorybulkadjustquantityatlocation-error-bad-request/td-p/1496764

Hi @amer2737 ,

So to get the delta:> 1) GET current Shopify stock> 2) GET current ERP stock> > 3) Compare and calculate delta (how many were sold, and what delta is that to the current stock)> 4) Push delta

There is nothing complicated in getting the delta here.

One bulk Graphql request to the current Shopify stock which is really fast - 20 seconds at most - then matching the shopify variant id or the sku with the values in the ERP stock, compare the quantities- takes probably less then one second with only 4000 variants, probably no need to monitor how many were sold, that is implicit seen when you compare the quantities. Push the deltas will only take 30 seconds at most. So you should be able to update your Shopify inventory items in less than a minute. The only bottleneck I can see is the call to your ERP.

As usual if you have a first version it is very often possible to speed it up by a factor two to ten.

Regards

Thomas

1 Like

I have optimized this now.

  • The call to ERP is negligible. 0.1 sec.
  • The calculation is trivial, but annoying as shopify should also have a flag to just overwrite instead of delta.
  • The call to the API is capped with 200 calls. So for 10.000 products this takes 1.5 minutes.
  • With Rest Api this is only possible one after the other and takes 10-15min. Also subpar that they would not let us to restapi the whole inventory instead of each called individually.
  • The cap is quite low and not sure why they slow us down. Easily could update all 10seconds.

It is just 300 products with variations making 10.000 lines. So a really small catalogue.

One thing that I forgot though that is really good about having delta updates available is to make sure to push only non 0 updates and therefore reducing the number of lines to update significantly.