How is a price rendered on a frontend product view page in Magento 2?

Magento 2 Development

Dmitry Fedyuk illustrates how a price is rendered on a frontend product view page in Magento 2 with the help of the following code:

Table of contents

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml#L55-L61

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Pricing/Render.php#L58

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/default.xml#L10-L16

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L92

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/catalog_product_prices.xml#L9-L38

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L98

Note that

  •  $priceCode is final_price on a catalog product view;
  • $type is a product type (e.g. grouped).

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L59

Ad as for $renderClassName, it may be \Magento\Catalog\Pricing\Render\FinalPriceBox.

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L227-L241

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/catalog_product_prices.xml#L23-L26

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L66

As for  $price, it may be \Magento\ConfigurableProduct\Pricing\Price\FinalPrice.

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Model/Product.php#L1079-L1090

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Model/Product/Type.php#L161-L170

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/PriceInfo/Factory.php#L56-L87

Note that:

magento/magento2/blob/2.0.0/app/code/Magento/ConfigurableProduct/etc/di.xml#L77-L81

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L79

As for $renderBlock, it may be \Magento\Catalog\Pricing\Render\FinalPriceBox.

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L85

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L210-L225

The expected result may look like Magento_Catalog::product/price/final_price.phtml.

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L99

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/final_price.phtml#L17

$priceModel not only supports the \Magento\/Framework\Pricing\Price\PriceInterface interface but also may be an instance of  the \Magento\ConfigurableProduct\Pricing\Price\ConfigurableRegularPrice class.

Note that a PHPDock comment for the $priceModel variable should be:

The $priceModel may be, for example, an instane of the \Magento\ConfigurableProduct\Pricing\Price\ConfigurableRegularPrice class, which is not a descendant of the \Magento\Catalog\Pricing\Price\RegularPrice class.

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/final_price.phtml#L44

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/PriceBox.php#L114-L125

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/PriceBox.php#L127-L140

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L142

As for $amountBlock, it could be an instance of the \Magento\Framework\Pricing\Render\Amount class.

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L148

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L187-L202

Possible result is  Magento_Catalog::product/price/amount/default.phtml

magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/amount/default.phtml#L24

magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/Amount.php#L214-L228

magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/PriceCurrency.php#L73-L82

magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/PriceCurrency.php#L114

magento/magento2/blob/2.0.0/app/code/Magento/Store/Model/Store.php#L940-L955

magento/magento2/blob/2.0.0/app/code/Magento/Store/Model/Store.php#L867-L886

magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/Currency.php#L277-L294

Source

More tips from Magento 2 cookbook