This is useful to display variant swatches on your products in the search result pages for example.
Since the integration stores variants, and not products directly, in the Algolia index you need to do extra work to display information of all variants for each product.
You can do this in one of the following ways:
-
Call the Algolia Search API
You can fetch information for all variants by making a search call while filtering on_tags:\"id:${product_id}\"
and settingdistinct
tofalse
. -
Call the Shopify storefront API
You can also fetch all variants of a product with the Shopify storefront API. -
Store all the variant data in a metafield
It’s also possible to add all the variant information to a metafield associated with a product and index it via Algolia.
Storing variant data in a metafield
The basic process to follow would be to:
- Fetch all the products one by one
You can do this via theGET /admin/api/2020-10/products.json
endpoint using cursor based pagination. - Create the desired data structure by going through the variants for the fetched products
You can use tags or metafields to store this information. Since tags have a limit of 255 characters, it’s best to use metafields containing a JSON string of the variants data. This way, the front end can decode the JSON and use the content. - Update the metafields or tags on the products one by one
You can do this with thePUT /admin/api/{version}/products/{product_id}.json
endpoint for tags, or thePOST admin/products/#{id}/metafields.json
endpoint for metafields. - Configure the Algolia app to pick up this information
Metafields can be configured from the Indexing tab of the app, while tags are indexed by default. Once completed, search results return this data in themeta
attribute.