How to get a variant from inventory item ID?

JoesIdeas
Shopify Expert
2266 207 600

I use the inventory_levels/connect webhook to know when a fulfillment service gets activated on a product.

 

However, the payload only has these fields:

- inventory_item_id

- location_id

- available

 

I tried this to get the product variant with the REST API, but it doesn't work (found in the forums, sounds like it used to be an optional parameter but no more):

variant = ShopifyAPI::Variant.all(inventory_item_id: inventory_item_id).first

How can you identify which variant is being edited when the inventory_levels_connect webhook is triggered?

• Creator of Order Automator (automate tagging, fulfillment, Amazon, notifications + more)
• Shopify developer for 10+ years, store owner for 7 years
• I also make guides like Shopify Automation Tips and How to Deal with Fraud / Chargebacks
Replies 2 (2)

ShopifyDevSup
Shopify Staff
1384 229 481

Hey @JoesIdeas - I was able to do a little digging into this and it looks like this is intentional due partly to how we've changed our shipping/fulfillment APIs to the "fulfillment orders" based workflow.  It isn't possible at the moment to pull the product variant information solely from the Inventory Item ID/location ID.

That said, if your app does have access to read/write products on a shop, one workaround you could use is applying a tag to a variant based on its fulfillment location and then query the product based on the tag that relates to the fulfillment location(more information here). Then, from there, you can pull the inventory item ID from relevant variant. It adds a few more steps for sure, so I'm also going to put together a report with your feedback here to pass onto our product team. I can't guarantee anything on that front, but they definitely will take any feedback into future feature consideration. 

Hope this helps - let me know if we can clarify anything on our end! 

 

Al | Shopify Developer Support

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

JoesIdeas
Shopify Expert
2266 207 600

Thanks for the idea, I love a good workaround suggestion but I don't think that would work, because it still relies on us getting notified when the user assigns the location to the variant (to know which product to tag). Asking them to first tag the variant before assigning a location and hope they remember is not a reasonable solution.

 

This doesn't have to do with fulfillment. To give context, if a user adds a variant to a fulfillment service location, action is needed in our database on that variant.


By not knowing the variant affected, it requires getting all products assigned to the location, and cycling through those until the matching inventory_item_id is found. This can be hundreds of products, requiring extra API calls. 

 

It seems logical to me that the resource being affected should be identified.

 

This trend to hide key data also causes a lot of extra API calls on fulfillment requests. I've seen a few posts about this in the forums, here's the one I made: https://community.shopify.com/c/shopify-apis-and-sdks/is-there-a-way-to-get-the-fulfillment-order-id... - you commented on this one, I appreciate that too but I just don't understand why an important piece of data (the item / resource affected) isn't listed in either of these. 

• Creator of Order Automator (automate tagging, fulfillment, Amazon, notifications + more)
• Shopify developer for 10+ years, store owner for 7 years
• I also make guides like Shopify Automation Tips and How to Deal with Fraud / Chargebacks