0

I have this code to get the price of related products in new order email:

<?php echo Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'price', $storeId); ?>

It outputs the price but in the incorrect format like this 19.9700 with extra zeros. I've tried several ways to get it to remove the extra zeros but everything I can find Google doesn't show how this is done with getAttributeRawValue. How can I get the show the correct price format like 19.97? Thank you

3
  • Are you aware that using the raw attribute value will not take special prices into account? Commented Mar 19, 2016 at 22:46
  • No i wasn't aware of that. How does one get the special price if the item has one?
    – Freejoy
    Commented Mar 21, 2016 at 11:54
  • The best way is to load the related products collection and directly include the price data with $collection->addPriceData(), then use $product->getFinalPrice() on the collection items. This fetches all price data from the price index and applies group price, special price etc. Commented Mar 21, 2016 at 12:34

1 Answer 1

2

You need to format the price as per your store currency for this. You can try with this code :

<?php echo Mage::helper('core')->formatPrice(Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'price', $storeId), false); ?>

Method getAttributeRawValue will return raw value from DB in which it is stored and since Magento stores decimal values upto 4 places after decimal point, you are getting those extra 0s.

1
  • It worked. Thank you
    – Freejoy
    Commented Mar 19, 2016 at 15:14

Your Answer

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

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