5

The data that I'm getting only contains the SKU numbers. I am trying to figure out how I can link these SKU numbers to the product variants in Shopify without the actual product id number.

Example data:

<Inventory ItemNumber="100B3001-B-01">
    <ItemStatus Status="Avail" Quantity="0" />
  </Inventory>
  <Inventory ItemNumber="100B3001-B-02">
    <ItemStatus Status="Avail" Quantity="0" />
  </Inventory>
  <Inventory ItemNumber="100B3001-B-03">
    <ItemStatus Status="Avail" Quantity="-1" />
    <ItemStatus Status="Alloc" Quantity="1" />
  </Inventory>
  <Inventory ItemNumber="100B3001-B-04">
    <ItemStatus Status="Avail" Quantity="-1" />
    <ItemStatus Status="Alloc" Quantity="1" />
  </Inventory>
user1512535
  • 51
  • 1
  • 1
  • 2
  • I've tried looking through their api product and variant pages. However, they all require knowing either the product id or the variant id first. http://api.shopify.com/order.html http://api.shopify.com/product_variant.html Short of having these, I can't seem to find any info on it. Google, stackoverflow, nada. – user1512535 Jan 14 '13 at 21:43
  • No choice then. I'm going to have to manually get a list of the entire product line and then keep track of all the variants. It works for this client, which only has ~15 products and ~400 variants. For clients that have more products, this won't scale very well. – user1512535 Jan 14 '13 at 22:08
  • 2
    In Shopify, SKU is nothing more than a string attribute of a variant. hence it is not a good candidate for search. All 4000 variants of a shop can have the same SKU. Refactor your algorithm to use variant ID's and you'll be mighty glad you did. – David Lazar Jan 14 '13 at 23:32

6 Answers6

18

Here's a delightful, condescending discussion from Shopify employees in 2011 asking why you can't just store the Shopify ID everywhere. The "stock-keeping unit" is a universal systems integration point and, in every system I've seen, each SKU uniquely maps to a product because words have meaning, but apparently not at Shopify.

Three years later, you seem to have two options.

One is to create a Fulfillment Service and provide a URL where Shopify will call you asking for stock levels on a SKU; this is probably the simplest solution, provided you have a Web server sitting somewhere where you can expose such a callback.

The second is to periodically page through all of the Products and store a mapping of the Shopify ID to a SKU somewhere, consulting your map when you need to do an update. Because most of our integrations are cron jobs and I'd like to keep them that way, I periodically ask for the products that have changed since the last run, and then update my mapping.

Nicholas Piasecki
  • 25,203
  • 5
  • 80
  • 91
4

As David Lazar points out in his comment, the ability to find a product based on its SKU is not currently supported in the Shopify API.

Edward Ocampo-Gooding
  • 2,782
  • 1
  • 23
  • 29
  • 1
    I know this is an old thread, but any update on this? It's really really difficult to integrate Shopify to other systems without this feature. An example is that we need to automatic create a shopify "buy button" on external sites based on which product the user is looking at. But this require the product variant id for the product the user is looking at, which is an internal shopify value which our external systems don't know. Which is why we use sku values to uniquely identify products. – MTilsted Dec 09 '15 at 13:47
3

EDIT: This is an unreliable option that I once used as a last resort. I wouldn't recommend using this but I will leave it here in case as someone may find it helpful.

You can use this API endpoint:

/admin/products/search.json?query=sku:abc123

I have only used it in the browser though, and I can't find any documentation for it. And it may stop working at any time.

Liam
  • 19,819
  • 24
  • 83
  • 123
  • I saw several post talking about use this search query although my case doesnt work and I should use .myshopify.com/search?view=json&q=sku:SKUID . Any suggestions? – Rafael Ruiz Tabares Mar 15 '17 at 20:17
  • This doesn't work anymore now. It yields `{"errors":"Not Found"}` . yikes, any other solution or hidden API? – nayiaw Dec 06 '17 at 09:33
  • Try the variants endpoint. Works for me. `/admin/variants/search.json?query=sku:XYZ0000` So does the products endpoint, albeit formed slightly differently: `/admin/products/search.json?query=XYZ0000` – Crayons May 20 '18 at 21:16
  • @Crayons This feature used to work and now only works for some people depending on when you created your store. THey migrated this to beta because it wasn't documented. Really stupid if you ask me but true nontheless. Here is the comment that explains it - Go to bottom comment.https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/api-product-variant-search-183152 – dave4jr Jun 13 '18 at 23:23
0

You can use

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

Old post, but still relevant. I had come up with a jQuery AJAX function to obtain product info from a SKU, as well as pull other additional info:

        function getProductInfo(productID) {
        console.log("Getting product info for " + productID + "...");
        $.ajax({
            url: window.Shopify.routes.root + 'admin/products/search.json?query=sku:' + productID,
            method: "GET",
            success: function (data) {
                let pid = data.products['0'].id;
                let pdpUrl = window.Shopify.routes.root + 'products/' + data.products['0'].handle;
                let imgid = data.products['0'].image.src;
                let quantity = data.products['0'].variants['0'].inventory_quantity;
                if (quantity == 0) {
                    $('form[data-pid=' + productID + '] button[type=submit]').text('OUT OF STOCK').attr('onclick', 'event.preventDefault();').css("background-color", "#c6c6c6");;
                    console.log("PRODUCT SOLD OUT: " + productID)
                }
                $('a[data-sku=' + productID + '] img').css('background-image', "url('" + imgid + "')");
                $('a[data-sku=' + productID + ']').attr('href', pdpUrl);
                $('form input[value=' + productID + ']').attr('value', pid);
                return;
            },
            error: function (error) {
                console.log('Cannot retrieve data from SKU ' + sku);
            }
        });
    };

I also left in some of the success actions to give you guys a good starting point if you wish to build on manipulating the DOM after the function runs. Just a note, this will obtain the parent ID of the product, if you wish to get the product variant ID, you would have to just change this:

let pid = data.products['0'].id;

To this:

let pid = data.products['0'].variants['0'].id;

NOTE: If there is no variant associated with the product, it will throw an undefined error.

thamind
  • 679
  • 5
  • 5
-1

Graph query on Shopify works for me:

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    Please don’t post code as screenshots. Instead, post as text, and format using Markdown (or the editor controls). – Jeremy Caney Sep 15 '22 at 00:30