skip to Main Content

I’m working on a Shopify project and need to find all the collections associated with a specific product using its Product ID. For example, if "Red T-shirt" is a product that belongs to multiple collections, I want to retrieve the list of those collections.

Is it possible to achieve this using the Shopify Storefront API? If yes, which query or endpoint should I use? Are there any specific parameters or best practices for this use case?

Any help or sample code would be greatly appreciated.

2

Answers


  1. In this link, you can easily find the topic Retrieve a list of all smart collections for a certain product_id

    # Session is activated via Authentication
    test_session = ShopifyAPI::Context.active_session
    
    ShopifyAPI::SmartCollection.all(
      session: test_session,
      product_id: "632910392",
    )
    
    Login or Signup to reply.
  2. With the SFAPI, use a query like this:

    query GetProduct($id: ID!) {
      product(id: $id) {
        collections(first: 250) {
          nodes {
            title
            description
            # ...more data as needed
          }
        }
      }
    }
    

    And the variable:

    {
      "id": "gid://shopify/Product/123"
    }
    

    On the admin panel, you can install the GraphiQL app to test both Storefront and Admin api: https://shopify.dev/docs/storefronts/headless/building-with-the-storefront-api/api-exploration/graphiql-storefront-api

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search