6

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();
2

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>
?>
  • 2
    Using object manager in Magento 2 is a bad practice. This answer should not be followed. – Paras Sood Dec 28 '17 at 12:31
  • The above getProductprice() can get the configurable product price??? – Jaisa Feb 22 '18 at 3:10
  • I got the product collection which consists both simple and configurable products. I need to get price for each products. If simple comes means it easily get the price. But in the case of configure product, how it will get the price – Jaisa Feb 22 '18 at 3:13
  • @Arun Karnawat, I got the price of the simple product by referring​ Ur code – Jaisa Feb 22 '18 at 3:15
  • Struggling to get configurable product price – Jaisa Feb 22 '18 at 3:15
3
// 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); 
1

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();
1

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); ?>
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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