FROM CACHE - en_header

How do I search for a product based on the SKU?

narzero
Shopify Partner
61 0 14

I want to search for products based on the SKU's.

I've tried:

1

ShopifyAPI::Product.find(sku: 'wi196217')

# => ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found.
from /Users/narzero/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:144:in `handle_response'

2

p = Curl.get('https://<REDACTED>:<REDACTED>@ubermart.myshopify.com/admin/products/search.json?query=sku:wi196217';) do |http|
  http.headers['Content-Type'] = 'text/plain'
  http.headers['Accept'] = '*/*'
end

puts p.body

# => "{\"errors\":\"[API] That action is not currently supported.\"}"

Method 2 works when using the Advanced Rest Client.

What should I try next?

Replies 5 (5)
Shayne
Shopify Staff
Shopify Staff
208 9 47

Hi Narek. Since the SKU is actually on a specific variant of a product, you'll have better luck with something like this : 

ShopifyAPI::Variant.where(sku: 'wi196217').first

 

Shayne | Developer Advocate @ 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

narzero
Shopify Partner
61 0 14

Hi Shayne,

That doesn't work. It returns a bunch of variants, sometimes the correct variant (the one that contains the matching sku) is in there and sometimes it just isn't.

Is there no other way to retrieve a product id based on the sku?

Cheers.

Sebastian_Niezg
Shopify Partner
8 0 0

Shayne, is there a way to do a not search? For example, I'd like all products with a sku that is not wi196217.

Hey_Jones_
Shopify Partner
24 0 0

It doesn't look like that's possible through the API. The only way you could do it would be to loop through the Products and Variants until you found a match, or store a local database with the Variant id, sku and product_id.

Hey_Jones_
Shopify Partner
24 0 0

This would be cool:

ShopifyAPI::Variant.find(:all, params: { :sku => 'SKU123' } )