JSON API
...
Examples
Batch Updates

Complex products

2min

In the Simple Products, we saw how to index products. Let's say you have a complex product like a Shirt, available in S and M sizes. The following example shows how to index this complex product. Some other fields are left out for simplicity.

[ { "id": "1", "type": "KLEVU_PRODUCT", "attributes": { "name": { "default": "Shirt" }, "image": { "default": { "url": "https://4.imimg.com/data4/AO/DA/MY-24693421/black-shirt.jpg" } }, "url": "https://test.myshopify.com/products/black-shirt", "shortDescription": { "default": "This is a black shirt available in different sizes." } } }, { "id": "2", "type": "KLEVU_PRODUCT", "relations": { "parentProduct": { "values": [ "1" ] } }, "attributes": { "name": { "default": "Shirt" }, "image": { "default": { "url": "https://4.imimg.com/data4/AO/DA/MY-24693421/black-shirt.jpg" } }, "price": { "USD": { "defaultPrice": 100, "salePrice": 80 }, "GBP": { "defaultPrice": 90, "salePrice": 70 } }, "url": "https://test.myshopify.com/products/black-shirt-s", "shortDescription": { "default": "Black Shirt" }, "size": "s" } }, { "id": "3", "type": "KLEVU_PRODUCT", "relations": { "parentProduct": { "values": [ "1" ] } }, "attributes": { "name": { "default": "Shirt" }, "image": { "default": { "url": "https://4.imimg.com/data4/AO/DA/MY-24693421/black-shirt.jpg" } }, "price": { "USD": { "defaultPrice": 100, "salePrice": 80 }, "GBP": { "defaultPrice": 90, "salePrice": 70 } }, "url": "https://test.myshopify.com/products/black-shirt-m", "shortDescription": { "default": "Black Shirt" }, "size": "m" } } ]

Here we have indexed 3 items, first the parent product, then the two variants of the product. A few points to note:

  • The parent product needs to be indexed before indexing the variants.
  • The “relations” field represents the relation of the item with other items in your catalog. Here we have used the “parentProduct" relation at the variant level to map them to the parent product.
  • “size” is a custom attribute used to provide different values of the variant.
  • Under prices, observe that the sale price is different for variants, but startPrice and toPrice are calculated based on all the variants of the complex product and indexed into all variants. This helps in rendering the startPrice in the product card on the website.
  • url”: If variants have variant specific URLs, then use that for indexing else use the same product URL for all variants.
  • Currently, as you index the parent product, it will also come in the search result.
  • All the attributes have to be repeated for all variants.
  • Start and To prices have to be calculated and sent for each variant. Due to this, if any variant being deleted causes a change in either of the two values, then all other variants need to be indexed again.



Updated 13 Nov 2024
Doc contributor
Doc contributor
Doc contributor
Doc contributor
Did this page help you?