ShopifyAPI get product variant from inventory_item_id

t_t2
Visitor
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!

Replies 2 (2)

csam
Shopify Staff (Retired)
267 40 51

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

 

 

To learn more visit the Shopify Help Center or the Community Blog.

t_t2
Visitor
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.