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.

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

  • Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX
  • Magento\Tax\Model\Config::DISPLAY_TYPE_INCLUDING_TAX
  • Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH

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:

  • Magento\Catalog\Pricing\Price\RegularPrice (regular_price)
  • Magento\Catalog\Pricing\Price\FinalPrice (final_price)
  • Magento\Catalog\Pricing\Price\TierPrice (tier_price)
  • Magento\Catalog\Pricing\Price\SpecialPrice (special_price)
  • Magento\Catalog\Pricing\Price\BasePrice (base_price)
  • Magento\Catalog\Pricing\Price\CustomOptionPrice (custom_option_price)
  • Magento\Catalog\Pricing\Price\ConfiguredPrice (configured_price)
  • Magento\Catalog\Pricing\Price\ConfiguredRegularPrice (configured_regular_price)
  • Magento\CatalogRuleStaging\Pricing\Price\CatalogRulePrice (catalog_rule_price)
  • Magento\Msrp\Pricing\Price\MsrpPrice (msrp_price)
  • Magento\Catalog\Pricing\Price\ConfiguredPrice (wishlist_configured_price)

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