A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I have a list of items that have to be updated daily on prices and inventory quantities, I already know how to do it individually with the code below:
Hi @dfrance 👋
Generally, we recommend using bulk mutations to update large volumes of data asychronously and the Shopify python library supports GraphQL requests for bulk operations.
Alternatively, you can use the `productVariantsBulkUpdate` mutation to update the variant prices in bulk, and `inventoryBulkAdjustQuantityAtLocation` mutation to bulk modify the inventory quantities directly. Below is an example using the python library:
session = shopify.Session(shop_url, api_version, access_token)
shopify.ShopifyResource.activate_session(session)
mutation = """
mutation ($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) {
inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {
inventoryLevels {
id
available
}
}
}
"""
inputs = {
"inventoryItemAdjustments": [
{
"inventoryItemId": "gid://shopify/InventoryItem/41900365676662",
"availableDelta": 2
},
{
"inventoryItemId": "gid://shopify/InventoryItem/41900365742198",
"availableDelta": 3
},
{
"inventoryItemId": "gid://shopify/InventoryItem/41900365807734",
"availableDelta": 4
}
],
"locationId": "gid://shopify/Location/61122510966"
}
shopify.GraphQL().execute(
query=mutation,
variables=inputs,
)
Hope that helps!
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
While this does help, what I want is to set and not adjust values, I found the mutation "inventorySetOnHandQuantities" but this doesn't do bulk updates from what I saw. is there any alternative to that?
I could of course register the present number on stock and do the difference top adjust to the new stock value. The problem is that this method allows for weird behavior since it will have a time window between getting the present stock value and the new stock value
HI
Is there a bulk mutation for updating inventory at a certain location id?
Checking the list the closest one could be productVariantUpdate
but the field
Hi, I already lose over 10 hours trying to make this work... no luck!
If I use the function update_products_in_bulk() with json_payload return the error =
Error updating batch 1: Status Code 400, Response: {"errors":{"inventory_item_id":"Required parameter missing or invalid"}}
Processed batch 1/1: {"errors":[{"message":"Field 'inventoryBulkAdjustQuantityAtLocation' doesn't exist on type 'Mutation'","locations":[{"line":3,"column":9}],"path":["mutation","inventoryBulkAdjustQuantityAtLocation"],"extensions":{"code":"undefinedField","typeName":"Mutation","fieldName":"inventoryBulkAdjustQuantityAtLocation"}},{"message":"Variable $inventoryItemAdjustments is declared by anonymous mutation but not used","locations":[{"line":2,"column":9}],"path":["mutation"],"extensions":{"code":"variableNotUsed","variableName":"inventoryItemAdjustments"}},{"message":"Variable $locationId is declared by anonymous mutation but not used","locations":[{"line":2,"column":9}],"path":["mutation"],"extensions":{"code":"variableNotUsed","variableName":"locationId"}}]}
this function only work with json_payload_b
Hi @daroan,
Looking at the api calls you are making with your code, the error you are receiving with the update_products_in_bulk() function is expected when you are using json_payload, as the REST inventory_levels/set.json endpoint, does only accept a single inventory item per call. This explains why the json_payload_b does work with this query, as it is only setting a single inventory item, where the json_payload input is trying to set multiple inventory items in a single call.
As for the error in the bulk_update_inventory() function, it appears this is due to the fact that the inventoryBulkAdjustQuantityAtLocation mutation query has been deprecated and fully removed as of API version 2024-04, I've confirmed this as well by testing on my own test store and I do receive the same error when making the call on version 2024-04 or later. To resolve this issue, you should switch to using the inventoryAdjustQuantities mutation instead, though if needed you can continue using the InventoryBulkAdjustQuantityAtLocation mutation as it is still available on API versions 2023-10 through 2024-01. However it will be completely unusable when these api versions are fully deprecated 1 year from their release.
Here's some relevant developer documentation to help with this further:
I hope this helps, and I hope you have a great day 🙂
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hi I very much appreciated the answer, was even faster than I could catch up... 😄
Tried update_products_in_bulk to update inventory in bulk with adjust.json does not worked, below is the data used before json.dumps({"inventory_levels" : batch}):
data= [{'location_id': 62155718791, 'inventory_item_id': 49236282179922, 'available': 0}, {'location_id': 62155718791, 'inventory_item_id': 49236282212690, 'available': 0}, {'location_id': 62155718791, 'inventory_item_id': 49236282245458, 'available': 0}, {'location_id': 62155718791, 'inventory_item_id': 42498772729991, 'available': 0}, {'location_id': 62155718791, 'inventory_item_id': 42535159529607, 'available': 0}]
Error updating batch 1: Status Code 400, Response: {"errors":{"inventory_item_id":"Required parameter missing or invalid"}}
===
On bulk_update_inventory I tried change the version to 2024-01 but also did not work, data used:
data= {'inventoryItemAdjustments': [{'inventoryLevelId': 'gid://shopify/InventoryItem/49236282179922', 'availableDelta': 0}, {'inventoryLevelId': 'gid://shopify/InventoryItem/49236282212690', 'availableDelta': 0}, {'inventoryLevelId': 'gid://shopify/InventoryItem/49236282245458', 'availableDelta': 0}, {'inventoryLevelId': 'gid://shopify/InventoryItem/42498772729991', 'availableDelta': 0}, {'inventoryLevelId': 'gid://shopify/InventoryItem/42535159529607', 'availableDelta': 0}], 'locationId': 'gid://shopify/Location/62155718791'}
Processed batch 1/1: {"errors":[{"message":"Field 'inventoryBulkAdjustQuantityAtLocation' doesn't exist on type 'Mutation'","locations":[{"line":3,"column":9}],"path":["mutation","inventoryBulkAdjustQuantityAtLocation"],"extensions":{"code":"undefinedField","typeName":"Mutation","fieldName":"inventoryBulkAdjustQuantityAtLocation"}},{"message":"Variable $inventoryItemAdjustments is declared by anonymous mutation but not used","locations":[{"line":2,"column":9}],"path":["mutation"],"extensions":{"code":"variableNotUsed","variableName":"inventoryItemAdjustments"}},{"message":"Variable $locationId is declared by anonymous mutation but not used","locations":[{"line":2,"column":9}],"path":["mutation"],"extensions":{"code":"variableNotUsed","variableName":"locationId"}}]}
😞