sku search return not found

rijo
New Member
3 0 0

admin/variants/search.json?query=sku:k0001 works fine on browser but not found on rest api call

Replies 5 (5)
olivert
Explorer
51 12 20
rijo
New Member
3 0 0

@olivert Thanks for the reply.

Actually what I need is to update the inventory quantity, for that I make an api call to  admin/inventory_levels/adjust.json

array(
"location_id"=> XXXXX,
"inventory_item_id"=> XXX,
"available_adjustment"=>10
)

for this I need "inventory_item_id" 

How do I get "inventory_item_id" with sku 

Can anyone help..

olivert
Explorer
51 12 20

Hi @rijo 

No worries.

You'll have to use the GraphQL API for that. 
the REST API will not allow you to query by SKU in this way. 

the below query searches by SKU, and returns the inventoryItem ID for the item returned.

Have a go at using the query below. you can use the GraphQL app in Shopify to test these queries out, before you put them in your code.
you will need read_inventory and read_products permissions to get this working.

you can supply query variables to your GraphQL query, so it will search the product variant according to your query

"sku: INSERTSKUHERE"

for example. if i use "sku: 1234" 
the query will return product variants that have a sku of "1234"

there are heaps of other fields available under the InventoryItem object. I've selected only ID
https://shopify.dev/docs/admin-api/graphql/reference/object/inventoryitem#fields-2020-04

 

query($query: String){
        productVariants(first: 1, query: $query) {
          edges{
            node{
              id
              inventoryQuantity
              inventoryItem{
                id
              }
            }
          }
        }
      }

 

You can also remove the query all together and just hard code it in there

      {
        productVariants(first: 1, query: "sku:1234") {
          edges{
            node{
              id
              inventoryQuantity
              inventoryItem{
                id
              }
            }
          }
        }
      }

if you scroll to the bottom of this page, 
they have a GraphQL app embedded in the web page which is very handy. 

https://shopify.dev/docs/admin-api/graphql/reference/object/product?api[version]=2020-04#samples

 

 

rijo
New Member
3 0 0

Hi @olivert 

I have tried the same on Shopify GraphiQL App  results

{
"data": null,
"errors": [
{
"message": "access denied",
"locations": [
{
"line": 7,
"column": 9
}
],
"path": [
"productVariants",
"edges",
0,
"node",
"inventoryItem"
]
}
],
"extensions": {
"cost": {
"requestedQueryCost": 4,
"actualQueryCost": 4,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 996,
"restoreRate": 50
}
}
}
}

When I remove inventoryItem{ id } results output

{
"data": {
"productVariants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/XXXXXXXXXXX",
"inventoryQuantity": 2
}
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 3,
"actualQueryCost": 3,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 997,
"restoreRate": 50
}
}
}
}

Don't know what is wrong with inventoryItem{ id }

--Thanks

 

olivert
Explorer
51 12 20

Are you trying this on the Shopify GraphQL App installed on a store?
or the one on their developer site? 

It's best to install the app on your store, and have a go there. 
just tried and the one publicly available on their website won't give the correct permissions.

Install it on a store and give it another go.