Magento 2: How to get the product price with currency symbol in PHTML file with help of block
-
03-10-2020 - |
Question
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();
La solution
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>
?>
Autres conseils
// 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);
You can get your current currency symbol from Magento\Directory\Model\Currency
so just intitialize it in your construct function and call method getCurrencySymbol()
for get current currecy symbol:
$currencyCode = $this->_currency->getCurrencySymbol();
According to this
vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml
You could just use this in order to get the price with the currency.
<?php echo $block->getProductPrice($product); ?>
On Magento 2 in my custom Theme, I'm simply using this on the product page in the phtml file.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$price = $product->getPrice();
Or this in any other phtml file.
The difference is because on this example I'm getting by product ID instead of the current product.
In this case, you can set the ID to bring the desired product price. On the example, I get the ID of the current product.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productid = $this->getRequest()->getParam('id');
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid);
$price = $product->getPrice();
And I'm using this code line to format price.
Before: 7.000
>>>> After: £7.00
$priceFormatted = $this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format($product->getPrice(), 2), true, false);