Get Product ID by Inventory Item ID

Solved

Get Product ID by Inventory Item ID

hmaesta
Shopify Partner
2 0 0

I need to set inventory levels for a specific location.

My current flow is:

 

1. Request locations/{locationId}/inventory_levels.json. This returns all the inventory_item_id's and current levels (parameter "available") for each item.

2. To make a match of what product this is, I request inventory_items.json?ids=inventory_item_id's that returns the sku for each inventory_item_id

3. With the SKU I can check my warehouse API, get current inventory levels, and update them on Shopify.

 

All fine… Right?

 

Not always, because some products don't have SKU. While creating the product, someone forgot to fill in the SKU input.

So now I need to create an alarm saying that this specific product doesn't have an SKU — and, for that, I need to find the product by the inventory_item_id.

 

How can I manage to do that using REST API?

Accepted Solution (1)

garyrgilbert
Shopify Partner
428 41 179

This is an accepted solution.

I believe the short answer is: You can't

 

The inventory_item is associated to a product variant, but there is no way to go from the inventory_item to the product variant directly. It would make sense to have a way to get back by including the variant_id but the only way is via the SKU.  And lets be honest, how can you find a product in a warehouse without an SKU after all SKU = Stock Keeping Unit so... no dice with the REST API

 

BUT... GraphQL to the rescue! Time to shake off your "fear" and dive in..

 

{
  inventoryItem(id:"gid://shopify/InventoryItem/41899753111757"){
    variant{
      product{
        id
      }
    }
  }
}

 

Does the trick and costs only 3:

 

{
  "data": {
    "inventoryItem": {
      "variant": {
        "product": {
          "id": "gid://shopify/Product/6665038725325"
        }
      }
    }
  }

 

Cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution

View solution in original post

Reply 1 (1)

garyrgilbert
Shopify Partner
428 41 179

This is an accepted solution.

I believe the short answer is: You can't

 

The inventory_item is associated to a product variant, but there is no way to go from the inventory_item to the product variant directly. It would make sense to have a way to get back by including the variant_id but the only way is via the SKU.  And lets be honest, how can you find a product in a warehouse without an SKU after all SKU = Stock Keeping Unit so... no dice with the REST API

 

BUT... GraphQL to the rescue! Time to shake off your "fear" and dive in..

 

{
  inventoryItem(id:"gid://shopify/InventoryItem/41899753111757"){
    variant{
      product{
        id
      }
    }
  }
}

 

Does the trick and costs only 3:

 

{
  "data": {
    "inventoryItem": {
      "variant": {
        "product": {
          "id": "gid://shopify/Product/6665038725325"
        }
      }
    }
  }

 

Cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution