123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Pricing\Render;
- use Magento\Framework\Pricing\Amount\AmountInterface;
- use Magento\Framework\Pricing\SaleableInterface;
- use Magento\Framework\Pricing\Price\PriceInterface;
- use Magento\Framework\Pricing\PriceCurrencyInterface;
- use Magento\Framework\View\Element\Template;
- /**
- * Price amount renderer
- *
- * @method string getAdjustmentCssClasses()
- * @method string getDisplayLabel()
- * @method string getPriceId()
- * @method bool getIncludeContainer()
- * @method bool getSkipAdjustments()
- */
- class Amount extends Template implements AmountRenderInterface
- {
- /**
- * @var SaleableInterface
- */
- protected $saleableItem;
- /**
- * @var PriceInterface
- */
- protected $price;
- /**
- * @var AdjustmentRenderInterface[]
- */
- protected $adjustmentRenders;
- /**
- * @var PriceCurrencyInterface
- */
- protected $priceCurrency;
- /**
- * @var RendererPool
- */
- protected $rendererPool;
- /**
- * @var AmountInterface
- */
- protected $amount;
- /**
- * @var null|float
- */
- protected $displayValue;
- /**
- * @var string[]
- */
- protected $adjustmentsHtml = [];
- /**
- * @param Template\Context $context
- * @param AmountInterface $amount
- * @param PriceCurrencyInterface $priceCurrency
- * @param RendererPool $rendererPool
- * @param SaleableInterface $saleableItem
- * @param \Magento\Framework\Pricing\Price\PriceInterface $price
- * @param array $data
- */
- public function __construct(
- Template\Context $context,
- AmountInterface $amount,
- PriceCurrencyInterface $priceCurrency,
- RendererPool $rendererPool,
- SaleableInterface $saleableItem = null,
- PriceInterface $price = null,
- array $data = []
- ) {
- parent::__construct($context, $data);
- $this->amount = $amount;
- $this->saleableItem = $saleableItem;
- $this->price = $price;
- $this->priceCurrency = $priceCurrency;
- $this->rendererPool = $rendererPool;
- }
- /**
- * @param float $value
- * @return void
- */
- public function setDisplayValue($value)
- {
- $this->displayValue = $value;
- }
- /**
- * @return float
- */
- public function getDisplayValue()
- {
- if ($this->displayValue !== null) {
- return $this->displayValue;
- } else {
- return $this->getAmount()->getValue();
- }
- }
- /**
- * @return AmountInterface
- */
- public function getAmount()
- {
- return $this->amount;
- }
- /**
- * @return SaleableInterface
- */
- public function getSaleableItem()
- {
- return $this->saleableItem;
- }
- /**
- * @return \Magento\Framework\Pricing\Price\PriceInterface
- */
- public function getPrice()
- {
- return $this->price;
- }
- /**
- * @return string
- */
- public function getDisplayCurrencyCode()
- {
- return $this->priceCurrency->getCurrency()->getCurrencyCode();
- }
- /**
- * @return string
- */
- public function getDisplayCurrencySymbol()
- {
- return $this->priceCurrency->getCurrencySymbol();
- }
- /**
- * @return bool
- */
- public function hasAdjustmentsHtml()
- {
- return (bool) count($this->adjustmentsHtml);
- }
- /**
- * @return string
- */
- public function getAdjustmentsHtml()
- {
- return implode('', $this->adjustmentsHtml);
- }
- /**
- * @return string
- */
- protected function _toHtml()
- {
- $adjustmentRenders = $this->getAdjustmentRenders();
- if ($adjustmentRenders) {
- $adjustmentHtml = $this->getAdjustments($adjustmentRenders);
- if (!$this->hasSkipAdjustments() ||
- ($this->hasSkipAdjustments() && $this->getSkipAdjustments() == false)) {
- $this->adjustmentsHtml = $adjustmentHtml;
- }
- }
- $html = parent::_toHtml();
- return $html;
- }
- /**
- * @return AdjustmentRenderInterface[]
- */
- protected function getAdjustmentRenders()
- {
- return $this->rendererPool->getAdjustmentRenders($this->saleableItem, $this->price);
- }
- /**
- * @param AdjustmentRenderInterface[] $adjustmentRenders
- * @return array
- */
- protected function getAdjustments($adjustmentRenders)
- {
- $this->setAdjustmentCssClasses($adjustmentRenders);
- $data = $this->getData();
- $adjustments = [];
- foreach ($adjustmentRenders as $adjustmentRender) {
- if ($this->getAmount()->getAdjustmentAmount($adjustmentRender->getAdjustmentCode()) !== false) {
- $html = $adjustmentRender->render($this, $data);
- if (trim($html)) {
- $adjustments[$adjustmentRender->getAdjustmentCode()] = $html;
- }
- }
- }
- return $adjustments;
- }
- /**
- * 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);
- }
- /**
- * @param AdjustmentRenderInterface[] $adjustmentRenders
- * @return array
- */
- protected function setAdjustmentCssClasses($adjustmentRenders)
- {
- $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
- $cssClasses = array_merge($cssClasses, array_keys($adjustmentRenders));
- $this->setData('adjustment_css_classes', join(' ', $cssClasses));
- return $this;
- }
- }
|