8

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

7 Answers 7

9

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>
?>
5
  • 4
    Using object manager in Magento 2 is a bad practice. This answer should not be followed.
    – Paras Sood
    Dec 28, 2017 at 12:31
  • The above getProductprice() can get the configurable product price???
    – Jaisa
    Feb 22, 2018 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, 2018 at 3:13
  • @Arun Karnawat, I got the price of the simple product by referring​ Ur code
    – Jaisa
    Feb 22, 2018 at 3:15
  • Struggling to get configurable product price
    – Jaisa
    Feb 22, 2018 at 3:15
9
// 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); 
2
  • 1
    You should never use ObjectManager directly in Magento. If you really need Pricing Helper, you can inject it to the block or use ->getHelper()
    – Łukasz
    Dec 7, 2020 at 12:22
  • @Łukasz thanks for the answer, anyway this was old thing, in 2017 hard to find help for magento 2, actually now I'm not doing magento
    – Lasantha
    Dec 8, 2020 at 17:00
6

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);
4
  • Thanks Rafael, your answer saved my time. Mar 11, 2020 at 13:44
  • @Rafael I tried both the option but it did not work for me. I want to get product details in amount/default.phtml file.
    – Nafsss
    Jun 10, 2020 at 8:44
  • @Nafisa Could you please post the full relative path and te error your are seeing? Jun 11, 2020 at 12:32
  • currency symbol is showing after the product price
    – Mohit Rane
    May 13, 2022 at 5:55
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); ?>
1

Getting the price for multiple products, without the usual HTML wrapping.

/** @var $priceHelper \Magento\Framework\Pricing\Helper\Data */
$priceHelper = $this->helper('Magento\Framework\Pricing\Helper\Data');
/** @var $_item \Magento\Catalog\Model\Product **/
foreach($_items as $_item){
    $finalPrice = $_item->getPriceInfo()
        ->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)
        ->getMinimalPrice()
        ->getValue();
    echo '<strong>Price: '.$priceHelper->currency($finalPrice, false, false).'</strong>';
}

This will print the price of all products, with the currency symbol for the current store as well as round to correct decimal places.

0

Try This Code

Block File

protected $resultLayout;
protected $_productloader;

public function __construct(
    ..............................
    \Magento\Catalog\Model\ProductFactory $_productloader,
    \Magento\Framework\View\Result\Layout $resultLayout,
    ..............................
) {
    ..............................
    $this->resultLayout = $resultLayout;
    $this->_productloader = $_productloader;
    ..............................
}

..............................

public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
{
    /** @var \Magento\Framework\Pricing\Render $priceRender */
    $priceRender = $this->resultLayout->getLayout()->getBlock(\Magento\Framework\Pricing\Render::class);
    if (!$priceRender) {
        $priceRender = $this->resultLayout->getLayout()->createBlock(
            \Magento\Framework\Pricing\Render::class,
            \Magento\Framework\Pricing\Render::class,
            ['data' => ['price_render_handle' => 'catalog_product_prices']]
        );
    }
    $price = '';
    if ($priceRender) {
        $price = $priceRender->render(
            \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
            $product,
            [
                'display_minimal_price' => true,
                'use_link_for_as_low_as' => true,
                'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
            ]
        );
    }

    return $price;
}

public function getProductData($id)
{
    return $this->_productloader->create()->load($id);
}

phtml File

$productid = 1088;

$product = $block->getProductData($productid);

echo $block->getProductPriceHtml($product);

I Hope This Helps You.

Your Answer

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

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