ShopifyAPI get product variant from inventory_item_id

t_t2
New Member
2 0 0

I'm having trouble using ShopifyAPI to get a variant from an inventory_item_id. Here's a sample response from ShopifyAPI::Variant.find(:all):

{"id"=>40232259485873, "title"=>"red", "price"=>"10.00", "sku"=>"1", "position"=>1, "inventory_policy"=>"deny", "compare_at_price"=>nil, "fulfillment_service"=>"manual", "inventory_management"=>"shopify", "option1"=>"red", "option2"=>nil, "option3"=>nil, "created_at"=>"2021-06-26T15:22:08-04:00", "updated_at"=>"2021-07-14T22:49:15-04:00", "taxable"=>true, "barcode"=>"", "grams"=>0, "image_id"=>nil, "weight"=>0.0, "weight_unit"=>"kg", "inventory_item_id"=>42322551111851, "inventory_quantity"=>8, "requires_shipping"=>true, "admin_graphql_api_id"=>"gid://shopify/ProductVariant/41111111111111"}

As you can see, the inventory_item_id is 42322551111851. But when I do this: product_variant = ShopifyAPI::Variant.find(:all, :params=> {:inventory_item_id => 42322551111851).first, I get the wrong variant which doesn't match with the inventory_item_id that I specified. If I remove :all I get the error message:

ActiveResource::BadRequest: Failed.  Response code = 400.  Response message = Bad Request (id; expected String to be a id).

 I'm using API version 2021-04. Any help would be great!

0 Likes
csam
Shopify Staff
Shopify Staff
113 15 18

Hi @t_t2 

I don't see a mismatch occurring here. Looking at the sample JSON you provided, I can see that the inventory item ID does match your query, and it's returning variant ID 40232259485873. These IDs are not meant to be the same, as they identify two different things. 

 

21-07-e5rga-261wq

 

 

CS | API Support @ 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

0 Likes
t_t2
New Member
2 0 0

Hi @csam 

The issue I'm having is that when I do:

 

product_variant = ShopifyAPI::Variant.find(:all, :params=> {:inventory_item_id => 42322551111851}).first

 

 I get a product variant with a different inventory_item_id than i specified in the params. If I remove .first, i get a list of all the variants.

0 Likes