Magento Imagine Awards 2019 Finalist

Getting The Right Price on Magento v2.x

One of the most common requirements of Magento development is outputting the right price within bespoke functionality. Depending on the backend configuration, simple $product->getPrice() the expression simply might not be enough to get the job done.

Selecting The Right Price in Magento 2

We’ll take a look at the backend price configuration and what expressions you’ll need to get the price right on Magento V2.x

The most popular way…

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 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.

$_priceInfo->getPrice('regular_price')->getAmount()->getValue();
$_priceInfo->getPrice('final_price')->getAmount()->getValue();
...

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

$rendererPool = \Magento\Framework\App\ObjectManager::getInstance()
->create(\Magento\Framework\Pricing\Render\RendererPool::class);

$rendererPool->setData([
'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()
]);

$priceBox->setTemplate('Magento_Catalog::product/price/final_price.phtml');

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>
</span>
</span>
</div>

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.

Talk to us

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

reCAPTCHA

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

Search engine optimisation (SEO) is an important factor for any website and should not be ignored. If you don’t know,…

If you’re running an ecommerce site on Magento 1, you should be aware by now that support for the long-running…

The 3rd annual Magento Imagine eConference took place in early April where over 1500 Magento merchants, developers and industry partners…

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