New Shopify Certification now available: Liquid Storefronts for Theme Developers

How to make bulk updates on prices and quantities via API on python?

dfrance
Shopify Partner
2 0 1

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:

 

for item in update_data.to_dict('records'😞
  #update price on variant
  p_variant = shopify.Product.find(item['id']).variants[0]
  #In case theres a need to update the tracking by api, by default the inventory management is "none", which doesn't allow for api updates
  p_variant.inventory_management = 'shopify'
  p_variant.price = item['Markup price']
  p_variant.save()  
  #update inventory quantity
  print(item['name'])
  shopify.InventoryLevel.set(location_id = location_id, 
                             inventory_item_id = item['inventory_item_id'],
                             available = item['Qnt available'])
  sleep(0.5)
 

But this is too slow and I have 10k+ itens to updates. I didn't find any function to make a mass update and everything that I found until now is either on another language or outdated. Any help is apreciated!
Replies 3 (3)
ShopifyDevSup
Shopify Staff
Shopify Staff
1202 190 420

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

dfrance
Shopify Partner
2 0 1

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

newbie_01
Shopify Partner
29 0 6

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

inventoryQuantities is only used for creating variants, not for updating them.