0

we have amended our Magento 2.1 to use and store prices with 4 decimal places. But then used some JS to trim/round-up on the frontend to 2 decimal places.

However, on the email templates, the price is displaying with 4 decimal places e.g. £15.99000

Can I override the default email template to format the number to 2 decimal places (since the email doesn't allow JS)

I am currently looking at

vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml

And the price is currently being called like this...

<td class="item-price"> <?= /* @escapeNotVerified */ $block->getItemPrice($_item); ?> </td>

I tried changing the above to this instead...

<?php echo number_format($_item->getItemPrice(), 2); ?>

But that just seems to zero out the price, however it does read with 2 decimal places. So instead of it being £15.99 I just get £0.00

Where is my replacement code going wrong or am I even in the right place?

Thanks

2
  • Is it possible you can include what you changed to get 4 dp working? Jun 11, 2019 at 0:17
  • Not fully but basically we used this extension as a starting point to set the system to use 4DP github.com/lillik/magento2-price-decimal then applied some JS to trim off the extra zeros - there is a slight moment where 4DP shows on frontend when the page loads but we'll live with it Jun 11, 2019 at 12:17

5 Answers 5

0

Mine seems to work well with using this code below.

<td class="item-price">
    <?= /* @escapeNotVerified */  $block->getItemPrice($_item->getOrderItem()) ?>
<td/>

Hopefully this will fix your problem

1
  • Thanks for replying - it didn't work unfortunately - after making this change I actually could not checkout at all Jun 11, 2019 at 11:38
0

That code should works

<td class="item-price">
    <?= /* @escapeNotVerified */  number_format($block->getItemPrice($_item), 2, '.', ''); ?>
</td> 
2
  • Thanks for replying - this seems to wipe out the item details completely on the email. So where there would be the items listed, there is nothing Jun 11, 2019 at 11:37
  • UPDATE: I am seeing this in debug.log when using the above [2019-06-11 12:29:56] main.CRITICAL: Warning: number_format() expects parameter 1 to be float, string given in /var/view_preprocessed/html/vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml on line 1 {"is_exception":false} [] Jun 11, 2019 at 12:31
0

I think what's happening is getItemPrice is pulling through more than just a number

https://github.com/magento/magento2/blob/0eb8677b0b4e35606032e856cc1ef7c80e68829f/app/code/Magento/Sales/Block/Order/Email/Items/Order/DefaultOrder.php#L99

So

<?php echo number_format(preg_replace('/[^0-9.]/', '', $_item->getItemPrice()), 2); ?>

Potentially you will lose currency symbol though

1
  • Thanks Dominic but again that seems to remove the full items including product name etc from the email. And I see same error in debug.log Warning: number_format() expects parameter 1 to be float, Could/should it be converted to a float before trying to call it somehow? Thanks Jun 11, 2019 at 14:50
0

I seem to have sorted it by using this instead...

Added new variable

$zx1 = $_item->getRowTotal();

Then just displayed that

<?php echo $zx1; ?>

Thanks to this question which pointed me in the right direction...

division of two variables is returning zero , in magento2 WHY?

0

I think when you use

<?php echo number_format($_item->getItemPrice(), 2); ?>

The code return " £15.99" not "15.99"

2
  • Yeah I don't think getItemPrice is a number/float so it can't convert it. Using getRowTotal works for me. It doesn't have the currency symbol but we are only single currency so I can just add one in manually. Jun 13, 2019 at 8:19
  • @RichardLaws yeah, good look
    – wightman
    Jun 13, 2019 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.