Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Amount.php
Go to the documentation of this file.
1 <?php
8 
14 
24 class Amount extends Template implements AmountRenderInterface
25 {
29  protected $saleableItem;
30 
34  protected $price;
35 
39  protected $adjustmentRenders;
40 
44  protected $priceCurrency;
45 
49  protected $rendererPool;
50 
54  protected $amount;
55 
59  protected $displayValue;
60 
64  protected $adjustmentsHtml = [];
65 
75  public function __construct(
76  Template\Context $context,
81  PriceInterface $price = null,
82  array $data = []
83  ) {
84  parent::__construct($context, $data);
85  $this->amount = $amount;
86  $this->saleableItem = $saleableItem;
87  $this->price = $price;
88  $this->priceCurrency = $priceCurrency;
89  $this->rendererPool = $rendererPool;
90  }
91 
96  public function setDisplayValue($value)
97  {
98  $this->displayValue = $value;
99  }
100 
104  public function getDisplayValue()
105  {
106  if ($this->displayValue !== null) {
107  return $this->displayValue;
108  } else {
109  return $this->getAmount()->getValue();
110  }
111  }
112 
116  public function getAmount()
117  {
118  return $this->amount;
119  }
120 
124  public function getSaleableItem()
125  {
126  return $this->saleableItem;
127  }
128 
132  public function getPrice()
133  {
134  return $this->price;
135  }
136 
140  public function getDisplayCurrencyCode()
141  {
142  return $this->priceCurrency->getCurrency()->getCurrencyCode();
143  }
144 
148  public function getDisplayCurrencySymbol()
149  {
150  return $this->priceCurrency->getCurrencySymbol();
151  }
152 
156  public function hasAdjustmentsHtml()
157  {
158  return (bool) count($this->adjustmentsHtml);
159  }
160 
164  public function getAdjustmentsHtml()
165  {
166  return implode('', $this->adjustmentsHtml);
167  }
168 
172  protected function _toHtml()
173  {
175  if ($adjustmentRenders) {
176  $adjustmentHtml = $this->getAdjustments($adjustmentRenders);
177  if (!$this->hasSkipAdjustments() ||
178  ($this->hasSkipAdjustments() && $this->getSkipAdjustments() == false)) {
179  $this->adjustmentsHtml = $adjustmentHtml;
180  }
181  }
182  $html = parent::_toHtml();
183  return $html;
184  }
185 
189  protected function getAdjustmentRenders()
190  {
191  return $this->rendererPool->getAdjustmentRenders($this->saleableItem, $this->price);
192  }
193 
198  protected function getAdjustments($adjustmentRenders)
199  {
201  $data = $this->getData();
202  $adjustments = [];
203  foreach ($adjustmentRenders as $adjustmentRender) {
204  if ($this->getAmount()->getAdjustmentAmount($adjustmentRender->getAdjustmentCode()) !== false) {
205  $html = $adjustmentRender->render($this, $data);
206  if (trim($html)) {
207  $adjustments[$adjustmentRender->getAdjustmentCode()] = $html;
208  }
209  }
210  }
211  return $adjustments;
212  }
213 
222  public function formatCurrency(
223  $amount,
224  $includeContainer = true,
226  ) {
227  return $this->priceCurrency->format($amount, $includeContainer, $precision);
228  }
229 
235  {
236  $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
237  $cssClasses = array_merge($cssClasses, array_keys($adjustmentRenders));
238  $this->setData('adjustment_css_classes', join(' ', $cssClasses));
239  return $this;
240  }
241 }
__construct(Template\Context $context, AmountInterface $amount, PriceCurrencyInterface $priceCurrency, RendererPool $rendererPool, SaleableInterface $saleableItem=null, PriceInterface $price=null, array $data=[])
Definition: Amount.php:75
setAdjustmentCssClasses($adjustmentRenders)
Definition: Amount.php:234
getData($key='', $index=null)
Definition: DataObject.php:119
if( $block->displayPriceExclTax()||$block->displayBothPrices())(__('Excl. Tax')) ?>"> <?php if ($block -> displayPriceWithWeeeDetails()): ?> <span class="cart-tax-total" data-mage-init=' Magento Weee Helper Data Magento Weee Helper Data title amount
Definition: unit.phtml:68
$value
Definition: gender.phtml:16
formatCurrency( $amount, $includeContainer=true, $precision=PriceCurrencyInterface::DEFAULT_PRECISION)
Definition: Amount.php:222
setData($key, $value=null)
Definition: DataObject.php:72
getAdjustments($adjustmentRenders)
Definition: Amount.php:198