How is a price rendered on a frontend product view page in Magento 2?
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
- 1 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml#L55-L61
- 2 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/default.xml#L10-L16
- 3 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L92
- 4 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/catalog_product_prices.xml#L9-L38
- 5 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L98
- 6 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L59
- 7 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L227-L241
- 8 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/catalog_product_prices.xml#L23-L26
- 9 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L66
- 10 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Model/Product.php#L1079-L1090
- 11 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Model/Product/Type.php#L161-L170
- 12 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/PriceInfo/Factory.php#L56-L87
- 13 magento/magento2/blob/2.0.0/app/code/Magento/ConfigurableProduct/etc/di.xml#L77-L81
- 14 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L79
- 15 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L85
- 16 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L99
- 17 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/final_price.phtml#L17
- 18 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/final_price.phtml#L44
- 19 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/PriceBox.php#L114-L125
- 20 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/PriceBox.php#L127-L140
- 21 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L142
- 22 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L148
- 23 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L187-L202
- 24 magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/amount/default.phtml#L24
- 25 magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/Amount.php#L214-L228
- 26 magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/PriceCurrency.php#L73-L82
- 27 magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/PriceCurrency.php#L114
- 28 magento/magento2/blob/2.0.0/app/code/Magento/Store/Model/Store.php#L940-L955
- 29 magento/magento2/blob/2.0.0/app/code/Magento/Store/Model/Store.php#L867-L886
- 30 magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/Currency.php#L277-L294
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml#L55-L61
1 2 3 4 5 6 7 |
<block class="Magento\Catalog\Pricing\Render" name="product.price.tier" after="product.info.price"> <arguments> <argument name="price_render" xsi:type="string">product.price.render.default</argument> <argument name="price_type_code" xsi:type="string">tier_price</argument> <argument name="zone" xsi:type="string">item_view</argument> </arguments> </block> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
*/ protected function _toHtml() { /** @var PricingRender $priceRender */ $priceRender = $this->getLayout()->getBlock($this->getPriceRender()); if ($priceRender instanceof PricingRender) { $product = $this->getProduct(); if ($product instanceof SaleableInterface) { $arguments = $this->getData(); $arguments['render_block'] = $this; return $priceRender->render($this->getPriceTypeCode(), $product, $arguments); } } return parent::_toHtml(); } /** * Returns saleable item instance * * @return Product */ |
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/default.xml#L10-L16
1 2 3 4 5 6 7 |
<block class="Magento\Framework\Pricing\Render" name="product.price.render.default"> <arguments> <argument name="price_render_handle" xsi:type="string">catalog_product_prices</argument> <argument name="use_link_for_as_low_as" xsi:type="boolean">true</argument> <!-- set "override" configuration settings here --> </arguments> </block> |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L92
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
* @param array $arguments * @return string * @throws \InvalidArgumentException * @throws \RuntimeException */ public function render($priceCode, SaleableInterface $saleableItem, array $arguments = []) { $useArguments = array_replace($this->_data, $arguments); /** @var \Magento\Framework\Pricing\Render\RendererPool $rendererPool */ $rendererPool = $this->priceLayout->getBlock('render.product.prices'); if (!$rendererPool) { throw new \RuntimeException('Wrong Price Rendering layout configuration. Factory block is missed'); } // obtain concrete Price Render $priceRender = $rendererPool->createPriceRender($priceCode, $saleableItem, $useArguments); return $priceRender->toHtml(); } /** |
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/catalog_product_prices.xml#L9-L38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices"> <arguments> <argument name="default" xsi:type="array"> <item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item> <item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item> <item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item> <item name="default_amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.phtml</item> <item name="prices" xsi:type="array"> <item name="special_price" xsi:type="array"> <item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item> </item> <item name="tier_price" xsi:type="array"> <item name="render_template" xsi:type="string">Magento_Catalog::product/price/tier_prices.phtml</item> </item> <item name="final_price" xsi:type="array"> <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item> <item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price.phtml</item> </item> <item name="custom_option_price" xsi:type="array"> <item name="amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.phtml</item> |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render.php#L98
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ $useArguments = array_replace($this->_data, $arguments); /** @var \Magento\Framework\Pricing\Render\RendererPool $rendererPool */ $rendererPool = $this->priceLayout->getBlock('render.product.prices'); if (!$rendererPool) { throw new \RuntimeException('Wrong Price Rendering layout configuration. Factory block is missed'); } // obtain concrete Price Render $priceRender = $rendererPool->createPriceRender($priceCode, $saleableItem, $useArguments); return $priceRender->toHtml(); } /** * Render price amount * * @param AmountInterface $amount * @param PriceInterface $price * @param SaleableInterface $saleableItem * @param array $arguments |
Note that
-
$priceCode
isfinal_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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
) { $type = $saleableItem->getTypeId(); // implement class resolving fallback $pattern = [ $type . '/prices/' . $priceCode . '/render_class', $type . '/default_render_class', 'default/prices/' . $priceCode . '/render_class', 'default/default_render_class', ]; $renderClassName = $this->findDataByPattern($pattern); if (!$renderClassName) { throw new \InvalidArgumentException( 'Class name for price code "' . $priceCode . '" not registered' ); } $price = $saleableItem->getPriceInfo()->getPrice($priceCode); if (!$price) { throw new \InvalidArgumentException( 'Price model for price code "' . $priceCode . '" not registered' |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * @param array $pattern * @return null|string */ protected function findDataByPattern(array $pattern) { $data = null; foreach ($pattern as $key) { $data = $this->getData($key); if ($data) { break; } } return $data; } |
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/layout/catalog_product_prices.xml#L23-L26
1 2 3 4 |
<item name="final_price" xsi:type="array"> <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item> <item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price.phtml</item> </item> |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
'default/prices/' . $priceCode . '/render_class', 'default/default_render_class', ]; $renderClassName = $this->findDataByPattern($pattern); if (!$renderClassName) { throw new \InvalidArgumentException( 'Class name for price code "' . $priceCode . '" not registered' ); } $price = $saleableItem->getPriceInfo()->getPrice($priceCode); if (!$price) { throw new \InvalidArgumentException( 'Price model for price code "' . $priceCode . '" not registered' ); } $arguments['data'] = $data; $arguments['rendererPool'] = $this; $arguments['price'] = $price; $arguments['saleableItem'] = $saleableItem; |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Get product Price Info object * * @return \Magento\Framework\Pricing\PriceInfo\Base */ public function getPriceInfo() { if (!$this->_priceInfo) { $this->_priceInfo = $this->_catalogProductType->getPriceInfo($this); } return $this->_priceInfo; } |
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Model/Product/Type.php#L161-L170
1 2 3 4 5 6 7 8 9 10 |
/** * Get Product Price Info object * * @param Product $saleableItem * @return \Magento\Framework\Pricing\PriceInfoInterface */ public function getPriceInfo(Product $saleableItem) { return $this->_priceInfoFactory->create($saleableItem); } |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/PriceInfo/Factory.php#L56-L87
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public function create(SaleableInterface $saleableItem, array $arguments = []) { $type = $saleableItem->getTypeId(); if (isset($this->types[$type]['infoClass'])) { $priceInfo = $this->types[$type]['infoClass']; } else { $priceInfo = $this->types['default']['infoClass']; } if (isset($this->types[$type]['prices'])) { $priceCollection = $this->types[$type]['prices']; } else { $priceCollection = $this->types['default']['prices']; } $arguments['saleableItem'] = $saleableItem; $quantity = $saleableItem->getQty(); if ($quantity) { $arguments['quantity'] = $quantity; |
Note that:
$priceInfo
may beMagento\Framework\Pricing\PriceInfo\Base ;$priceCollection
–\Magento\ConfigurableProduct\Pricing\Price\Collection .
magento/magento2/blob/2.0.0/app/code/Magento/ConfigurableProduct/etc/di.xml#L77-L81
1 2 3 4 5 |
<virtualType name="Magento\ConfigurableProduct\Pricing\Price\Collection" type="Magento\Framework\Pricing\Price\Collection"> <arguments> <argument name="pool" xsi:type="object">Magento\ConfigurableProduct\Pricing\Price\Pool</argument> </arguments> </virtualType> |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L79
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
'Price model for price code "' . $priceCode . '" not registered' ); } $arguments['data'] = $data; $arguments['rendererPool'] = $this; $arguments['price'] = $price; $arguments['saleableItem'] = $saleableItem; /** @var \Magento\Framework\View\Element\Template $renderBlock */ $renderBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments); if (!$renderBlock instanceof PriceBoxRenderInterface) { throw new \InvalidArgumentException( 'Block "' . $renderClassName . '" must implement \Magento\Framework\Pricing\Render\PriceBoxRenderInterface' ); } $renderBlock->setTemplate($this->getRenderBlockTemplate($type, $priceCode)); return $renderBlock; } /** |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$arguments['price'] = $price; $arguments['saleableItem'] = $saleableItem; /** @var \Magento\Framework\View\Element\Template $renderBlock */ $renderBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments); if (!$renderBlock instanceof PriceBoxRenderInterface) { throw new \InvalidArgumentException( 'Block "' . $renderClassName . '" must implement \Magento\Framework\Pricing\Render\PriceBoxRenderInterface' ); } $renderBlock->setTemplate($this->getRenderBlockTemplate($type, $priceCode)); return $renderBlock; } /** * Create amount renderer * * @param AmountInterface $amount * @param SaleableInterface $saleableItem * @param PriceInterface $price * @param array $data |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
protected function getRenderBlockTemplate($type, $priceCode) { $pattern = [ $type . '/prices/' . $priceCode . '/render_template', $type . '/default_render_template', 'default/prices/' . $priceCode . '/render_template', 'default/default_render_template', ]; $template = $this->findDataByPattern($pattern); if (!$template) { throw new \InvalidArgumentException( 'Price code "' . $priceCode . '" render block not configured' ); } return $template; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$useArguments = array_replace($this->_data, $arguments); /** @var \Magento\Framework\Pricing\Render\RendererPool $rendererPool */ $rendererPool = $this->priceLayout->getBlock('render.product.prices'); if (!$rendererPool) { throw new \RuntimeException('Wrong Price Rendering layout configuration. Factory block is missed'); } // obtain concrete Price Render $priceRender = $rendererPool->createPriceRender($priceCode, $saleableItem, $useArguments); return $priceRender->toHtml(); } /** * Render price amount * * @param AmountInterface $amount * @param PriceInterface $price * @param SaleableInterface $saleableItem * @param array $arguments * @return string |
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/view/base/templates/product/price/final_price.phtml#L17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// @codingStandardsIgnoreFile ?> <?php /** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */ $productId = $block->getSaleableItem()->getId(); /** @var \Magento\Catalog\Pricing\Price\RegularPrice $priceModel */ $priceModel = $block->getPriceType('regular_price'); /** @var \Magento\Catalog\Pricing\Price\FinalPrice $finalPriceModel */ $finalPriceModel = $block->getPriceType('final_price'); $idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : ''; $schema = ($block->getZone() == 'item_view') ? true : false; ?> <?php if ($block->hasSpecialPrice()): ?> <span class="special-price"> <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [ 'display_label' => __('Special Price'), |
$priceModel not only
supports the \Magento\/Framework\Pricing\Price\PriceInterface
\Magento\ConfigurableProduct\Pricing\Price\ConfigurableRegularPrice
Note that a PHPDock comment for the $priceModel variable should be:
1 |
/** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */ |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="old-price"> <?php /* @escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [ 'display_label' => __('Regular Price'), 'price_id' => $block->getPriceId('old-price-' . $idSuffix), 'price_type' => 'oldPrice', 'include_container' => true, 'skip_adjustments' => true ]); ?> </span> <?php else: ?> <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [ 'price_id' => $block->getPriceId('product-price-' . $idSuffix), 'price_type' => 'finalPrice', 'include_container' => true, 'schema' => $schema ]); ?> <?php endif; ?> <?php if ($block->showMinimalPrice()): ?> <?php if ($block->getUseLinkForAsLowAs()):?> <a href="<?php /* @escapeNotVerified */ echo $block->getSaleableItem()->getProductUrl(); ?>" class="minimal-price-link"> |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/PriceBox.php#L114-L125
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * @param AmountInterface $amount * @param array $arguments * @return string */ public function renderAmount(AmountInterface $amount, array $arguments = []) { $arguments = array_replace($this->getData(), $arguments); //@TODO AmountInterface does not contain toHtml() method return $this->getAmountRender($amount, $arguments)->toHtml(); } |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/PriceBox.php#L127-L140
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * @param AmountInterface $amount * @param array $arguments * @return AmountRenderInterface */ protected function getAmountRender(AmountInterface $amount, array $arguments = []) { return $this->rendererPool->createAmountRender( $amount, $this->getSaleableItem(), $this->getPrice(), $arguments ); } |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L142
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$arguments['amount'] = $amount; if ($saleableItem) { $arguments['saleableItem'] = $saleableItem; if ($price) { $arguments['price'] = $price; } } /** @var \Magento\Framework\View\Element\Template $amountBlock */ $amountBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments); if (!$amountBlock instanceof AmountRenderInterface) { throw new \InvalidArgumentException( 'Block "' . $renderClassName . '" must implement \Magento\Framework\Pricing\Render\AmountRenderInterface' ); } $amountBlock->setTemplate($this->getAmountRenderBlockTemplate($type, $priceCode)); return $amountBlock; } /** |
As for $amountBlock, it
could be an instance of the \Magento\Framework\Pricing\Render\Amount
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L148
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
} } /** @var \Magento\Framework\View\Element\Template $amountBlock */ $amountBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments); if (!$amountBlock instanceof AmountRenderInterface) { throw new \InvalidArgumentException( 'Block "' . $renderClassName . '" must implement \Magento\Framework\Pricing\Render\AmountRenderInterface' ); } $amountBlock->setTemplate($this->getAmountRenderBlockTemplate($type, $priceCode)); return $amountBlock; } /** * @param SaleableInterface $saleableItem * @param PriceInterface $price * @return array */ public function getAdjustmentRenders(SaleableInterface $saleableItem = null, PriceInterface $price = null) { |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/RendererPool.php#L187-L202
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
protected function getAmountRenderBlockTemplate($type, $priceCode) { $pattern = [ $type . '/prices/' . $priceCode . '/amount_render_template', $type . '/default_amount_render_template', 'default/prices/' . $priceCode . '/amount_render_template', 'default/default_amount_render_template', ]; $template = $this->findDataByPattern($pattern); if (!$template) { throw new \InvalidArgumentException( 'For type "' . $type . '" amount render block not configured' ); } return $template; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php echo $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>> <?php if ($block->getDisplayLabel()): ?> <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span> <?php endif; ?> <span <?php if ($block->getPriceId()): ?> id="<?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif;?> <?php echo($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?> data-price-amount="<?php /* @escapeNotVerified */ echo $block->getDisplayValue(); ?>" data-price-type="<?php /* @escapeNotVerified */ echo $block->getPriceType(); ?>" class="price-wrapper <?php /* @escapeNotVerified */ echo $block->getPriceWrapperCss(); ?>" <?php echo $block->getSchema() ? ' itemprop="price"' : '' ?>> <?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?> </span> <?php if ($block->hasAdjustmentsHtml()): ?> <?php echo $block->getAdjustmentsHtml() ?> <?php endif; ?> <?php if ($block->getSchema()): ?> <meta itemprop="priceCurrency" content="<?php /* @escapeNotVerified */ echo $block->getDisplayCurrencyCode()?>" /> <?php endif; ?> </span> |
magento/magento2/blob/2.0.0/lib/internal/Magento/Framework/Pricing/Render/Amount.php#L214-L228
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Format price value * * @param float $amount * @param bool $includeContainer * @param int $precision * @return float */ public function formatCurrency( $amount, $includeContainer = true, $precision = PriceCurrencyInterface::DEFAULT_PRECISION ) { return $this->priceCurrency->format($amount, $includeContainer, $precision); } |
magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/PriceCurrency.php#L73-L82
1 2 3 4 5 6 7 8 9 10 |
public function format( $amount, $includeContainer = true, $precision = self::DEFAULT_PRECISION, $scope = null, $currency = null ) { return $this->getCurrency($scope, $currency) ->formatPrecision($amount, $precision, [], $includeContainer); } |
magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/PriceCurrency.php#L114
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
if ($currency instanceof Currency) { $currentCurrency = $currency; } elseif (is_string($currency)) { $currency = $this->currencyFactory->create() ->load($currency); $baseCurrency = $this->getStore($scope) ->getBaseCurrency(); $currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency; } else { $currentCurrency = $this->getStore($scope) ->getCurrentCurrency(); } return $currentCurrency; } /** * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope * @param \Magento\Framework\Model\AbstractModel|string|null $currency * @return string */ |
magento/magento2/blob/2.0.0/app/code/Magento/Store/Model/Store.php#L940-L955
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public function getCurrentCurrency() { $currency = $this->getData('current_currency'); if ($currency === null) { $currency = $this->currencyFactory->create()->load($this->getCurrentCurrencyCode()); $baseCurrency = $this->getBaseCurrency(); if (!$baseCurrency->getRate($currency)) { $currency = $baseCurrency; $this->setCurrentCurrencyCode($baseCurrency->getCode()); } $this->setData('current_currency', $currency); } return $currency; } |
magento/magento2/blob/2.0.0/app/code/Magento/Store/Model/Store.php#L867-L886
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public function getCurrentCurrencyCode() { // try to get currently set code among allowed $code = $this->_httpContext->getValue(Context::CONTEXT_CURRENCY); $code = $code === null ? $this->_getSession()->getCurrencyCode() : $code; if (empty($code)) { $code = $this->getDefaultCurrencyCode(); } if (in_array($code, $this->getAvailableCurrencyCodes(true))) { return $code; } // take first one of allowed codes $codes = array_values($this->getAvailableCurrencyCodes(true)); if (empty($codes)) { // return default code, if no codes specified at all return $this->getDefaultCurrencyCode(); } return array_shift($codes); } |
magento/magento2/blob/2.0.0/app/code/Magento/Directory/Model/Currency.php#L277-L294
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public function formatPrecision( $price, $precision, $options = [], $includeContainer = true, $addBrackets = false ) { if (!isset($options['precision'])) { $options['precision'] = $precision; } if ($includeContainer) { return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt( $price, $options ) . ($addBrackets ? ']' : '') . '</span>'; } return $this->formatTxt($price, $options); } |
More tips from Magento 2 cookbook