Ad

Listing Shopify Products Via API From Multiple Collections

- 1 answer

I know how to list products from a specific custom collection using APi

...products.json?collection_id=xxxx

How do I get to list products from two or three specific collections? If there a way of telling API to list products that are found only in these collections, i. e. collection A and collection B only?

Ad

Answer

This is accomplished through the Collect API. Products are not directly associated with Custom Collections- they are associated with Collects, which are then associated with Custom Collections. So, with the Collect API, you obtain all Collects that are associated with a Custom Collection, and then retrieve all products associated with those Collects. You can even obtain these products in a single request (shown below; see gotchas).

See here in the documentation:

Getting all Collects for a certain Custom Collection

Getting a list of specific products


Example:

(Since we're only concerned about obtaining the product IDs from the collects, we can pass product_id to the fields parameter for faster response times)

GET /admin/collects.json?collection_id=841564295&fields=product_id

Response:

{
  "collects": [
    {
      "product_id": 921728736,
    },
    {
      "product_id": 632910392,
    }
  ]
}

GET /admin/products.json?ids=921728736,632910392

Response:

{
    "products": [ ... ]
}

Gotchas:

There is a default limit of 50 items per respone for both the Collect and Product endpoints, so you will need to handle pagination. You can up this 50 limit to 250 with limit=250 which will reduce your overall number of requests.

While there is no limit to how many IDs you specify with the ids parameter, the Product endpoint will still respect this default or specified limit. So, it's best to group your IDs into groups of 250, pass them separated by commas as the value for ids, then remember to add the limit=250 parameter to avoid having to paginate.

Ad
source: stackoverflow.com
Ad