简体   繁体   中英

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

I'm using the Ruby wrapper for Shopify's API and I want to search for products based on the SKU's.

Here's what I'm currently trying:

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'

I've also tried:

require 'curb'

p = Curl.get('https://<REDACTED>:<REDACTED>@<REDACTED>.myshopify.com/admin/products/search.json?query=sku:wi196217')

puts p.body

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

What should I try next?

The search URL requires a cookie to work (that only the admin has) this cookie is set when you login to the shopify site. So when trying to access it publicly via the API it returns

501 : {"errors":"[API] That action is not currently supported."}

To get past this I forked the gem and wrote a hack that allows you to search products through the gem.

https://github.com/achadee/shopify_api

I would not recommend using this for large inventory stores as it pulls down all the products down before searching... anyway usage is like so:

ShopifyAPI::Product.search({:sku => "XFUS-63912"})

ShopifyAPI::Product.search({:sku => "XFUS-63912", :handle => "hey hey"})

Also it dynamically searches any tag on both the product and all its variants

enjoy :)

First thing I noticed is that sku is a property of Product Variants , not Products .

This Stack Overflow answer indicates that it's not possible to get a product based on its sku with the Shopify API, but I found this discussion on the Shopify forums that may be useful to you.

您可以使用

*.myshopify.com/search?view=json&type=product&q=sku:SKUID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM