Search code examples
shopwareshopware6shopware6-api

How to find product based on configurator selection shopware 6 store-api


Using the store-api product query

 /store-api/product/31d688d6feed4cf4912e33fadff525e7

the response would look something like:

{
    "apiAlias": "product_detail",
    "product": {
        "parentId": "31d688d6feed4cf4912e33fadff525e7",
        "name": " test",
        "children": [],
        "media": [
            {
                "extensions": {
                    "foreignKeys": {
                        "apiAlias": "array_struct"
                    }
                },
                "_uniqueIdentifier": "522c74ae0b3e46d1832727f8fc86905f",
                "versionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
                "translated": [],
                "createdAt": "2023-02-09T11:57:02.459+00:00",
                "updatedAt": null,
                "productId": "2304c5f0965e45e49021666fa3d2e8fe",
                "mediaId": "ca2e6251ecfc4a31aeecdc11afd99599",
                "position": 1,
                "media": {
                    "url": "something.jpg",
                    "apiAlias": "media"
                },
                "id": "522c74ae0b3e46d1832727f8fc86905f",
                "customFields": null,
                "apiAlias": "product_media"
            },
            {
                "extensions": {
                    "foreignKeys": {
                        "apiAlias": "array_struct"
                    }
                },
                "_uniqueIdentifier": "76cefe6de215467e87eec41e72c1a258",
                "versionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
                "translated": [],
                "createdAt": "2023-02-09T11:57:02.459+00:00",
                "updatedAt": null,
                "productId": "2304c5f0965e45e49021666fa3d2e8fe",
                "mediaId": "7e9299aa047e40c2ac23b83cba6bd150",
                "position": 2,
                "media": {
                    "url": "some.jpg",
                    "apiAlias": "media"
                },
                "id": "76cefe6de215467e87eec41e72c1a258",
                "customFields": null,
                "apiAlias": "product_media"
            },
            {
                "extensions": {
                    "foreignKeys": {
                        "apiAlias": "array_struct"
                    }
                },
                "_uniqueIdentifier": "8b6197b7d10148d9ad6976f8cb4cf823",
                "versionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
                "translated": [],
                "createdAt": "2023-02-09T11:57:02.460+00:00",
                "updatedAt": null,
                "productId": "2304c5f0965e45e49021666fa3d2e8fe",
                "mediaId": "ef5a39b06baa427286307b05abac2af6",
                "position": 3,
                "media": {
                    "url": "some.jpg",
                    "apiAlias": "media"
                },
                "id": "8b6197b7d10148d9ad6976f8cb4cf823",
                "customFields": null,
                "apiAlias": "product_media"
            },
            {
                "extensions": {
                    "foreignKeys": {
                        "apiAlias": "array_struct"
                    }
                },
                "_uniqueIdentifier": "a86528ac563e42cdb035e7bfc3b39642",
                "versionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
                "translated": [],
                "createdAt": "2023-02-09T11:57:02.459+00:00",
                "updatedAt": null,
                "productId": "2304c5f0965e45e49021666fa3d2e8fe",
                "mediaId": "7a510ca3a6e64dd69b0ff48e5684b07c",
                "position": 0,
                "media": {
                    "url": "son.jpg",
                    "apiAlias": "media"
                },
                "id": "a86528ac563e42cdb035e7bfc3b39642",
                "customFields": null,
                "apiAlias": "product_media"
            }
        ],
        "configuratorSettings": [],
        "id": "2304c5f0965e45e49021666fa3d2e8fe",
        "apiAlias": "product"
    },
    "configurator": [
        {
            "name": "staerke",
            "options": [
                {
                    "name": "6 mm",
                    "id": "2ce160be8ba542838a40dba9f2ebdb76",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "7 mm",
                    "id": "a0aa0e9ac614422994983dc4cbfb06fc",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "8 mm",
                    "id": "7482f2571225472ca5b2cbe946711193",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "10 mm",
                    "id": "ed2b7ecfcd534fbfb578183a2a0c1fbb",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "13 mm",
                    "id": "ed862236787a46fa9dd5650d9599d812",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "16 mm",
                    "id": "4a928ecb176244ad87233f1c20957f86",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "20 mm",
                    "id": "484d6f1c4690450d941ddddc2344663c",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "22 mm",
                    "id": "a37d8779299147df9637fac52c307a22",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "26 mm",
                    "id": "7255ca2d4b9a49d4aabf501095b5ed98",
                    "apiAlias": "property_group_option"
                }
            ],
            "id": "7a2ca0a697b14a4abb2b6846e4faa879",
            "apiAlias": "property_group"
        },
        {
            "name": "Gewicht",
            "options": [
                {
                    "name": "2360 kg",
                    "id": "b6cb31b67d8f4135814d307e39074cce",
                    "apiAlias": "property_group_option"
                },
              
                {
                    "name": "11200 kg",
                    "id": "ba5f425d279e4a5abe16a331f6a0f76b",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "17000 kg",
                    "id": "b81453f199ac40f8b8a2bd60d43214c2",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "26500 kg",
                    "id": "5a1edc0039584a98b84f5682ff17692f",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "31500 kg",
                    "id": "c05979fbbf2a4d6b9baee8560cd1eaa6",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "45000 kg",
                    "id": "aeabc807a1c241699285d7a528970068",
                    "apiAlias": "property_group_option"
                }
            ],
            "id": "85e649d4685c41c08fbe49d719b53b35",
            "apiAlias": "property_group"
        },
        {
            "name": "last",
            "options": [
                {
                    "name": "1700 kg",
                    "id": "db4bd3d1dddc48cda42041edd84a2b6f",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "2240 kg",
                    "id": "9d886470139142438072729038bba99b",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "3000 kg",
                    "id": "3e4b332557b2481fb8778394398c2efe",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "4750 kg",
                    "id": "0d6081010c2744d79506f8c10d425fa5",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "8000 kg",
                    "id": "d0fe7d78f7584ab881f930f45490bbde",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "11800 kg",
                    "id": "dfec2bdabf204977a5d72da6e8bbbae3",
                    "apiAlias": "property_group_option"
                },
  
            ],
            "id": "6d23f7dfcd2841bb85ef1e1cd4fb7ba4",
            "apiAlias": "property_group"
        },
        {
            "name": "material",
            "options": [
                {
                    "name": "Aufhängering",
                    "id": "38b78090da3a464faaed833a545fa411",
                    "apiAlias": "property_group_option"
                },
                {
                    "name": "Gabelkopf-Lasthaken",
                    "id": "dd2496d345304416afa72a91a91ffa85",
                    "apiAlias": "property_group_option"
                },
                
            ],
            "id": "6a1b951061a545f289429621b0da5588",
            "apiAlias": "property_group"
        }
    ]
}

Based on the configurator object I can create buttons with Id's etc.

But I can't figure out how to get the product based on the configurator selection.

Do I filter for something like where parentId equals XXX and options.id equals xxx and so on? Not every combination is possible.


Solution

  • Do I filter for something like where parentId equals XXX and options.id equals xxx and so on?

    Yes, you fetch all variants via the parentId. They have a simple list property optionIds containing the ids of all associated property_group_option entities. You can then iterate the configurator property of the detail response, picking an option from each group included in a variant's optionIds. This should give you all available combinations.

    // POST /store-api/product
    {
        "includes": {
            "product": ["id", "optionIds"]
        },
        "filter": [
            {
                "type": "multi",   
                "operator": "and",
                "queries": [
                    {
                        "type": "equals",
                        "field": "parentId",
                        "value": "31d688d6feed4cf4912e33fadff525e7"
                    },
                    {
                        "type": "equals",
                        "field": "active",
                        "value": true
                    }
                ]
            }
        ]
    }
    

    Response example

    {
        "elements": [
            {
                "optionIds": [
                    "2bfc8642db854f97946409b2ca53e8b2",
                    "cd3658a077cf43d2bbb9d2b6df886703"
                ],
                "id": "0acd197599d94aa29d645c0e618ba049",
                "apiAlias": "product"
            },
            // ...
        ],
        "aggregations": [],
        "page": 1,
        "limit": 100,
        "entity": "product",
        "total": 16,
        "states": [],
        "apiAlias": "dal_entity_search_result"
    }
    

    Proof of concept

    const combinations = [];
    childrenResponse.elements.forEach((child) => {
      const combination = [];
      detailResponse.configurator.forEach((group) => {
        group.options.forEach((option) => {
          if (child.optionIds.includes(option.id)) {
            combination.push({
              group: group.name,
              option: option.name
            });
          }
        });
      });
      combinations.push({
        productId: child.id,
        variation: combination
      });
    });
    console.log(combinations);
    
    [
        {
            productId: "0acd197599d94aa29d645c0e618ba049",
            variation: [{group: "size", option: "40"}, {group: "skin", option: "scarlet"}]
        }, 
        {
            productId: "170f6e8ddb7e44ff94365d748469fee3",
            variation: [{group: "size", option: "35"}, {group: "skin", option: "scarlet"}]
        }, 
        {
            productId: "2ea96a7ce99c4418bf273676d3fb592f",
            variation: [{group: "size", option: "30,5"}, {group: "skin", option: "mediumturquoise"}]
        },
        // ...
    ]