InstantSearch - Show price, based on condition from array?

Hello, Im implementing InstantSearch.js in my ecommerce. It works well, quick and all, but got one problem- it’s how I store the price in my index.

The issue is that my “column” for price, is an array, and this array contains x items, based on the availability of the product.

E.g showing a price for Sweden, and one for US, because there’s different resellers in each region. Here’s an example:

  "availability": [
{
  "cid": "166",
  "p": 70,
  "sdf": 2,
  "sdt": 3,
  "sr": 5
},
{
  "cid": "196",
  "p": 70,
  "sdf": 2,
  "sdt": 3,
  "sr": 5
}

],

“cid” == country_id, “p” = price.

When showing this in PHP, when using just the API, Im just iterating each item and whenever I can find cid == current_country_id, I just show that price. But I also got a fallback, if “cid”==*, it means we have a “global shipping provider”.

Anyway, what Im trying to ask is if there’s a way to use the pricing column someway even though it’s an array?

e.g: {{availability.cid:136 || availability.cid:*}} or something? Maybe there’s something I can do in the template for the product?

Hi @mathias1, your best bet to achieve what you want would be to “denormalize” your data inside Algolia (or just flatten it) so that your objects will look like:

{objectID: ..., cid196p: 50, cid166p: 30}

Then on your frontend, if you already know the country id when the page loads, you can use the price widget and compute the right attribute to use.

Does that makes sense?

Hello Vvo, yes that does make sense! I actually achieved the results by the template parameter in the hits widget by returning a string;

Something like this:

			Algolia.addWidget(
			instantsearch.widgets.hits({
				container: '#hits',
				templates: 
				{
					item: function(_item)//document.getElementById('hit-template').innerHTML,
					{
						var list_price = null;
						var product_url = "/" + _item.name_url + "/" + page_key_product + "/" + App.AlphaId.encode(_item.objectID);

						var _template = "<figure><a href='"+product_url+"'><img src='//static.bithippo.com/images/products/640x480/"+_item.image_name+"' /></a></figure>";
							_template += "<div class='box-product-meta'>";
								_template += "<h4><a href='"+product_url+"'>" + _item._highlightResult.name.value + "</a></h4>";
								_template += "<p class='box-product-brand'>By: <a class='href-theme' href='/"+_item.brand.name_url + "/" + page_key_brand + "'>"+_item._highlightResult.brand.name.value+"</a></p>";

Yeah I think you get the point! Regarding flattening it out, the problem is still that this product which Im referring to might have either;

cid196p OR cidp ( = worldwide). This is in consideration when I do the filtering, but the problem is that cid196p might not exist…

But yes, I get what you say, I could however just iterate all countries with their prices, and those who should be from the *-price, I could in our backend just put that price in cid716p or whatever.

Thank you!