Not able to use if condition inside sum handlebar's helper

Hi there,

I come into situation when I want to do sum of tax rates but only when tax amount is greater than zero... For instance, you can have four different tax rates in US, however for shipping only two-three of them are applied, but in the payload all four are available so I want to filter out rates with zero amount.

origin handlebar without conditions:

{{#if shipping_lines_1.price}}
{{multiply 
(sum shipping_lines_1.tax_lines.[0].rate 
shipping_lines_1.tax_lines.[1].rate 
shipping_lines_1.tax_lines.[2].rate 
shipping_lines_1.tax_lines.[3].rate)
100}}
{{/if}}

part of the payload regarding "shipping_lines":

{"shipping_lines_1":
{"id": 1111111111,"carrier_identifier": null,"code": "Standard Shipping","delivery_category": null,"discounted_price": "8.90","discounted_price_set": {"shop_money": {"amount": "8.90","currency_code": "USD"},"presentment_money": {"amount": "8.90","currency_code": "USD"}},"phone": null,"price": "8.90","price_set": {"shop_money": {"amount": "8.90","currency_code": "USD"},"presentment_money": {"amount": "8.90","currency_code": "USD"}},"requested_fulfillment_service_id": null,"source": "shopify","title": "Standard Shipping",
    "tax_lines": [
    {"price": 0.09,
    "price_set": {"shop_money": {"amount": "0.09","currency_code": "USD"},"presentment_money": {"amount": "0.09","currency_code": "USD"}},
    "rate": 0.0625,
    "title": "IL STATE TAX"},
    {"price": 0.00,
    "price_set": {"shop_money": {"amount": "0.00","currency_code": "USD"},"presentment_money": {"amount": "0.00","currency_code": "USD"}},
    "rate": 0,
    "title": "IL COUNTY TAX"},
    {"price": 0.00,
    "price_set": {"shop_money": {"amount": "0.00","currency_code": "USD"},"presentment_money": {"amount": "0.00","currency_code": "USD"}},
    "rate": 0.01,
    "title": "IL CITY TAX"},
    {"price": 0.07,
    "price_set": {"shop_money": {"amount": 0.07,"currency_code": "USD"},"presentment_money": {"amount": "0.07","currency_code": "USD"}},
    "rate": 0.0075,
    "title": "IL SPECIAL TAX"}],
    "discount_allocations": []
    }}

as you can see the tax code with a name "IL CITY TAX" has rate 0.01 however amount is zero, which leads into miscalculation in the accounting system when imported shipping tax rate is 8% instead of 7%

So I've added conditions to check tax amount/price at the first place and then do sum of rates when tax amount is greater than zero:

{{#if shipping_lines_1.price}}
    {{multiply 
    (sum
    ({{#if shipping_lines_1.tax_lines.[0].price}}{{shipping_lines_1.tax_lines.[0].rate}}{{else}}0{{/if}})
     ({{#if shipping_lines_1.tax_lines.[1].price}}{{shipping_lines_1.tax_lines.[1].rate}}{{else}}0{{/if}})
     ({{#if shipping_lines_1.tax_lines.[2].price}}{{shipping_lines_1.tax_lines.[2].rate}}{{else}}0{{/if}})
     ({{#if shipping_lines_1.tax_lines.[3].price}}{{shipping_lines_1.tax_lines.[3].rate}}{{else}}0{{/if}})
    )100
    }}
    {{else}}0{{/if}}

Unfortunately, I always get an error: " Could not compile handle bar ... Parse error on line 4 ... Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'OPEN_BLOCK'"

My question here is: is there any Handlebars limitation, am I dumb I don't see missing parenthesis, should I take other approach for sum function or anything else?

Do you have any ideas for handlebars to correct the issue, please? Thank you in advance.

Hi, @dominikantalik. That error reflects a handlebars limitation. Control-flow block helpers like #if cannot be placed inside helper methods like sum().

You might want to consider an alternative approach, such as...

  • Checking for the tax value in a #compare statement before you do the math. The downside here is that #compare applies only to one value at a time. Then, you would likely have to nest comparisons levels down to cover the cases of more than one value at zero.
  • Starting over with JavaScript, as a hook or in a transform, to add a summary tax field to each record.

I was also wondering whether it might help to round certain rates down, if it's known that they have a negligible effect on the total.