use include with associations in shopware store-api

Viewed 26

I'm fetching all the parent products from the store-api including children/variants. In my shop the only difference between the parent and the children/variants is the stock and the name.

For the children i add the following association, and it returns the children ( with api_alias product)

{
  "page":1,
  "limit" : 100,
  "filter":[
    {
      "type":"equals",
      "field":"product.parentId",
      "value":null
    }],
  "associations":{
    "children":{
      "associations":{
        "options":{}
      }
    },
    "properties":{
      "associations":{
        "group":{}
      }
    }
  },
  "total-count-mode":1
}

I only need the stock and variant name, but it returns all the data like price, images etc.

I can use includes to define what i want back based on the api_alias. But this is product as well. So when i add the stock and options the rest also dissapears on the parent product.

"includes": {
      "product" : ["children", "stock", "options"]
      
 }

I also tried dot notation

"includes": {
      "product" : ["children.options", "children.stock"]
      
 }

That doesn't work either

How can i combine associations with includes, so that on the children i only get stock and options back. ( if this is even possible)

2 Answers

Sadly that is not possible. The includes are applied to all object of that type in the response, and there is not distinction where they appear in the response.

So the workaround for you would be to include all fields for products that you need on the parent and the children and combine them.

The includes collection refers to every instance of the given entity within the serialized json. You can't differentiate whether the entity was serialized in the context of a relation or not.

You could fetch parents only (where parentId is null) check if their childCount is greater 0 to find whether they have variants and then fetch the variants with their respective parentId and use the include in that separate query.

Related