Solved

Cannot Update Inventory Levels

TravisImagine
Shopify Partner
15 0 2

Hello,

 

I am having an issue, where I have a created a new location, and when I go to update the inventory levels for all the locations, the newly created location is not getting an inventory level created for it.

 

I am using this GraphQL query:

 

mutation productUpdate($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      id
    	variants (first: 10) {
        edges {
          node {
            inventoryItem {
              inventoryLevels (first: 10){
								edges{
                  node{
                    location{
                      name
                    }
                  }
                }               	 
              }
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

 

With the following input:

 

{
  "input": {
    "id": "gid://shopify/Product/123",
    "variants": [
      {
        "inventoryQuantities": [
          {
            "locationId": "gid://shopify/Location/1",
            "availableQuantity": 0
          },
          {
            "locationId": "gid://shopify/Location/2",
            "availableQuantity": 0
          },
          {
            "locationId": "gid://shopify/Location/3",
            "availableQuantity": 0
          },
          {
            "locationId": "gid://shopify/Location/4",
            "availableQuantity": 0
          }
        ],
        "options": [
          "WHITE",
          "6",
          "M"
        ]
      }
    ]
  }
}

 

 

The response I receive just has 1 inventory level location in it. I am expecting the item to update and place all 4 locations' inventory into it. Is this the expected result? I am wondering if there is some way to insert/create the inventory level/locations for all the locations while doing an update.

 

Thank you,

Travis

Accepted Solution (1)

Liam
Shopify Staff
2731 302 783

This is an accepted solution.

Hi Travis,

Yes, your understanding is correct. Usually, when you update a product's inventory, it should reflect the changes across all the locations. However, this might not happen for a newly created location because new locations do not automatically get inventory levels for all existing inventory items.

 

The inventory level is a link between an inventory item and a location, and this link is not created until you explicitly set an inventory level at the new location for the item. You can do this using the inventoryActivate mutation, as shown below:

mutation ActivateInventoryItem($inventoryItemId: ID!, $locationId: ID!, $available: Int) { 
    inventoryActivate(inventoryItemId: $inventoryItemId, locationId: $locationId, available: $available) { 
        inventoryLevel { 
            id 
            available 
            item { 
                id 
            } 
            location { 
                id 
            } 
        } 
    } 
}

You should run this mutation once for each inventory item that you want to be available at the new location. After you've created inventory levels at the new location, your productUpdate mutation should work as expected.

 

Please note that you should replace $inventoryItemId, $locationId, and $available with the respective values you want to set.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me 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

View solution in original post

Replies 2 (2)

Liam
Shopify Staff
2731 302 783

This is an accepted solution.

Hi Travis,

Yes, your understanding is correct. Usually, when you update a product's inventory, it should reflect the changes across all the locations. However, this might not happen for a newly created location because new locations do not automatically get inventory levels for all existing inventory items.

 

The inventory level is a link between an inventory item and a location, and this link is not created until you explicitly set an inventory level at the new location for the item. You can do this using the inventoryActivate mutation, as shown below:

mutation ActivateInventoryItem($inventoryItemId: ID!, $locationId: ID!, $available: Int) { 
    inventoryActivate(inventoryItemId: $inventoryItemId, locationId: $locationId, available: $available) { 
        inventoryLevel { 
            id 
            available 
            item { 
                id 
            } 
            location { 
                id 
            } 
        } 
    } 
}

You should run this mutation once for each inventory item that you want to be available at the new location. After you've created inventory levels at the new location, your productUpdate mutation should work as expected.

 

Please note that you should replace $inventoryItemId, $locationId, and $available with the respective values you want to set.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me 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

TravisImagine
Shopify Partner
15 0 2

Thanks Liam.

 

That is what I expected.

 

Quick follow-up question, I am trying to get the inventory items, so I can see if each sku has the correct amount of locations. Not every SKU in each product is the same, but often start with a similar pattern. I am trying to run:

 

query {
  inventoryItems(first: 1, query: "sku:test*") {
    edges {
      node {
        id
        tracked
        sku
        locationsCount
      }
    }
  }
}

 

I am expecting all SKUs that start with test, however I get "query not supported". If I change the syntax to just `query"sku:*"` then I do get the first result, since I'm only grabbing the first 1. Using `query"sku:'test*'"` returns an empty dataset, where I would expect to get the first result where the SKU starts with test.

 
I can move this to a new topic if that helps.
 
Thanks
Travis