How to find all the collections that your product belongs to on Shopify

Shopify’s REST API is a powerful tool for managing your store. It gives your apps direct access to product catalogues, customer records, and orders for just about any analysis you can imagine. However, the path to the data you need is not always obvious. For instance, it’s easy to find a product inside of a collection, but what if you need a list of collections that your product belongs to?

This is really useful data if you have a lot of product collections, whether they are created by hand or generated automatically. The answer to this question is not found in the Products endpoint of the rest api. There are two endpoints dedicated to collections, Collection, and CustomCollection. The first one retrieves automatic collections, the kind that include products that have special tags or other criteria. CustomCollection covers collections where the products have been added by hand or by a custom app.

You can find custom collections that contain your product by providing the product id.

GET /admin/api/2021-04/custom_collections.json?product_id=632910392

If you are using the Shopify Rest API gem, this line of code will do.

current_collections = ShopifyAPI::CustomCollection.find(:all, :params => {:product_id =>})

This also works for the collect API as well.

GET /admin/api/2021-04/collects.json?product_id=632910392

And here’s the ruby code for that operation.

current_collections = ShopifyAPI::Collection.find(:all, :params => {:product_id =>})

Now that you have the JSON data back from the API, you can pretty much do what you like with it. You can put the collections in separate lists, you can combine them, or you can cross-reference it and remove the product from collections it shouldn’t belong to.

I hope that allows somebody to take little bit more control of their Product Data. If you want to get more tips like these, feel free to subscribe to my newsletter!

Leave a Reply

Your email address will not be published. Required fields are marked *