Search code examples
prestashopsmarty

Smarty operation in Prestashop 1.7


I'm trying to do in Prestashop 1.7 math operation between two smarty variables inside product-discounts.tpl but result is wrong...

{$product.regular_price}

[output: 12,85 €]

{$quantity_discount.discount}

[output: 9.8%]

{$product.regular_price*$quantity_discount.discount}

[output: 117,6] Should be: 12.85*9.8= 125.93

Any idea?

I've tryed:

{$product.regular_price|floatval}

[output:12] Should be: 12.85

Thanks in advance

Prestashop 1.7


Solution

  • Actually, this is happening because of comma in price 12,85 €, so, if you replace the comma with dot then you will get a correct value 125.93

    I think the easiest solution would be by assigning a new variable in tpl file and replace the comma from the regular_price 12,85 with a dot.

    By the way, you could also replace comma with dots from the controller but if you want to perform mathematical operations within the template file you could do it like this:

    Firstly, assign {$product.regular_price} to regularPrice variable along with replacing comma with dot, something like this:

    {assign var=regularPrice value=$product.regular_price|replace:',':'.'}
    

    I hope $quantity_discount.discount always contains dot but if there is also a comma instead of dot then,

    {assign var=quantityDiscount value=$quantity_discount.discount|replace:',':'.'}
    

    and, last thing you have to do is multiple both variables regularPrice and quantityDiscount with each other

    {assign var=total_price value=$regularPrice * $quantityDiscount}
    

    then display total_price in tpl file like this:

    {$total_price}