Magento 2 Product Prices – Get Product Price with Currency Symbol in PHTML

magento2product-prices

From the $product object I am getting the collection of product from block and to render the fields. I want to get the price with it's currency code like
$99.00 in product listing.

Right now, I am using the below code to get the price of the product but i need to display the price with currency symbol.

$product->getFinalPrice();

Best Answer

create a object of abstractProduct Block then call the getProductPrice method and pass product object as parameter. see below.

$abstractProductBlock = $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\AbstractProduct');
echo $abstractProductBlock->getProductPrice($product);

Complete implementation example:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$abstractProductBlock = $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\AbstractProduct');

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToSort('created_at', 'DESC')
            ->setPageSize(8)
            ->load();
?>
<div>
    <?php foreach ($collection as $product) :?>
        <div class="item-box">
            <a href="<?php echo $product->getProductUrl(); ?>" >
                <span class="imgbx">
                    <img src="<?php echo $abstractProductBlock->getImage($product, 'latest_collection_list')->getImageUrl(); ?>" alt="<?php echo $product->getName(); ?>" />
                </span>
                <h3><?php echo $product->getName(); ?></h3>
                <span class="hm-price"><?php echo $abstractProductBlock->getProductPrice($product) ?></span>
            </a>
        </div>
    <?php endforeach;  ?>
</div>
?>
Related Topic