0

I am using Handlebars.js and I am trying to compare the price of each product to see if it is over $35. If so, I will display text beneath. The problem is that the price is stored as "$54.99", so when compared to a number it is treated as 0. How can I compare these two values?

Here is my code so far:

{{#gt price.without_tax.formatted 34.99}}
  This product qualifies for free shipping!
{{else}}
  Not free shipping
{{/gt}}
Jake P
  • 121
  • 1
  • 2
  • 9

2 Answers2

0

There is no problem with handlebars itself, but with a data you are passing to comparison, in this example price.without_tax.formatted.

Of course you can convert the price according to this answer: How to convert a currency string to a double with jQuery or Javascript? but anyway you should have raw price (as number) somewhere stored. If no- then there is application design issue I believe.

Maciej Kwas
  • 6,169
  • 2
  • 27
  • 51
  • I have not been able to locate anywhere that there is a raw price unfortunately. I saw the question you linked, but how can I take that value from handlebars and use it in a JS equation? – Jake P Aug 02 '17 at 21:29
  • I understand that you cannot register handlebars helpers? – Maciej Kwas Aug 02 '17 at 21:57
  • As @MaciejKwas said you have to create a helper and put your JS code in. Then use the helper you've created in your handlebar template to compare the results. – Christophe Aug 03 '17 at 13:34
0

The numeric field is accessed via: price.without_tax.value

Use that instead of price.without_tax.formatted and it should work.

  • Thank you, this was what I was looking for. BigCommerce does not allow helpers, so this was the best solution, otherwise the JS solution would have also worked. – Jake P Sep 05 '17 at 15:50