Magento Imagine Awards 2019 Finalist

Getting The Right Price on Magento v2.x

One of the most common requirement of Magento development is outputting the right price within bespoke functionality.

Selecting The Right Price in Magento 2

Depending on the backend configuration, simple $product->getPrice() expression simply might not be enough to get the job done.

Backend price config can vary in several ways; most notably by the value of $taxHelper->getConfig()->getPriceDisplayType():

Whereas the $taxHelper is of type Magento\Tax\Helper\Data.

This is why we should familiarize ourselves with getPriceInfo method of Magento\Catalog\Model\Product, implemented as follows:

public function getPriceInfo()
if (!$this->_priceInfo) {
$this->_priceInfo = $this->_catalogProductType->getPriceInfo($this);
return $this->_priceInfo;

The $this->_catalogProductType of type Magento\Catalog\Model\Product\Type ensures that the getPriceInfo is called properly for each product type we might be using (virtual, simple, configurable, …).

The getPriceInfo returns the result of Magento\Framework\Pricing\PriceInfoInterface interface, implemented via Magento\Framework\Pricing\PriceInfo\Base class.

There we have a getPrices method which really depicts the power of Magento pricing implementation.

This method further returns the collection of Magento\Framework\Pricing\Price\Collection type, which contains the instances of following types:

We can easily get the above output via a code such as:

$priceInfo = $_product->getPriceInfo();

$prices = $priceInfo->getPrices();

foreach ($prices as $k => $_price) {
echo sprintf('%s (%s)', get_class($_price), $k);

We can now use this information to precisely output the price value we need.


Likewise, we can use this information to output the entire HTML price box as follows.

$rendererPool = \Magento\Framework\App\ObjectManager::getInstance()

'default' =>
'default_amount_render_class' => \Magento\Framework\Pricing\Render\Amount::class,
'default_amount_render_template' => 'Magento_Catalog::product/price/amount/default.phtml',

$price = $priceInfo->getPrice('regular_price');

$priceBox = \Magento\Framework\App\ObjectManager::getInstance()
->create(\Magento\Catalog\Pricing\Render\FinalPriceBox::class, [
'saleableItem' => $_product,
'price' => $price,
'rendererPool' => $rendererPool,
'amount' => $price->getAmount()


echo $priceBox->toHtml();

Mind you, the use of object manager here is merely to provide cleaner copy-paste example, otherwise one should properly inject the dependencies through corresponding class __construct.

The above example should give us an output much like the following one.

<div class="price-box price-regular_price" data-role="priceBox" data-product-id="30" data-price-box="product-id-30">
<span class="price-container">
<span id="product-price-30" data-price-amount="6.99" data-price-type="finalPrice" class="price-wrapper ">
<span class="price">£6.99</span>

There is plenty more to be said about prices. The above however, should be enough to set us on the right track.

By studying each specific price type in depth we can easily output any necessary pricing information we need.

This article was written using Magento ver. 2.2.6.

Need more help with getting the right price on Magento?  Don’t hesitate to get in touch.

Subscribe via email

Sign up to get notifications of new posts by email.

Share this article

Posted in Development, Magento

Tagged , ,

Other posts you might like

Magento out of the box has many features such as the multi-store (multiple front-ends, one back-end) system which we are…

Upstream Commerce have announced they are integrating their Retail Intelligence Suite with Magento. Providing Magento users with the ability to effortlessly keep track…

Magento 2 – what’s new? As Magento Partners, it’s a question we’ve become accustomed to. Sure, Magento 1 is already jam…

Call us on 01785 279920

Our friendly Magento experts are happy to answer your questions.

Follow us!

Get the latest eCommerce news, reviews and expert advice in your inbox.

To find out more about how we use your data, read our Privacy Policy