Magento 2: How to get the product price with currency symbol in PHTML file with help of block
// Instance of Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// Instance of Pricing Helper
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
echo $priceHelper->currency($product->getFinalPrice(), true, false);
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>
?>