Amount.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Render;
  7. use Magento\Framework\Pricing\Amount\AmountInterface;
  8. use Magento\Framework\Pricing\SaleableInterface;
  9. use Magento\Framework\Pricing\Price\PriceInterface;
  10. use Magento\Framework\Pricing\PriceCurrencyInterface;
  11. use Magento\Framework\View\Element\Template;
  12. /**
  13. * Price amount renderer
  14. *
  15. * @method string getAdjustmentCssClasses()
  16. * @method string getDisplayLabel()
  17. * @method string getPriceId()
  18. * @method bool getIncludeContainer()
  19. * @method bool getSkipAdjustments()
  20. */
  21. class Amount extends Template implements AmountRenderInterface
  22. {
  23. /**
  24. * @var SaleableInterface
  25. */
  26. protected $saleableItem;
  27. /**
  28. * @var PriceInterface
  29. */
  30. protected $price;
  31. /**
  32. * @var AdjustmentRenderInterface[]
  33. */
  34. protected $adjustmentRenders;
  35. /**
  36. * @var PriceCurrencyInterface
  37. */
  38. protected $priceCurrency;
  39. /**
  40. * @var RendererPool
  41. */
  42. protected $rendererPool;
  43. /**
  44. * @var AmountInterface
  45. */
  46. protected $amount;
  47. /**
  48. * @var null|float
  49. */
  50. protected $displayValue;
  51. /**
  52. * @var string[]
  53. */
  54. protected $adjustmentsHtml = [];
  55. /**
  56. * @param Template\Context $context
  57. * @param AmountInterface $amount
  58. * @param PriceCurrencyInterface $priceCurrency
  59. * @param RendererPool $rendererPool
  60. * @param SaleableInterface $saleableItem
  61. * @param \Magento\Framework\Pricing\Price\PriceInterface $price
  62. * @param array $data
  63. */
  64. public function __construct(
  65. Template\Context $context,
  66. AmountInterface $amount,
  67. PriceCurrencyInterface $priceCurrency,
  68. RendererPool $rendererPool,
  69. SaleableInterface $saleableItem = null,
  70. PriceInterface $price = null,
  71. array $data = []
  72. ) {
  73. parent::__construct($context, $data);
  74. $this->amount = $amount;
  75. $this->saleableItem = $saleableItem;
  76. $this->price = $price;
  77. $this->priceCurrency = $priceCurrency;
  78. $this->rendererPool = $rendererPool;
  79. }
  80. /**
  81. * @param float $value
  82. * @return void
  83. */
  84. public function setDisplayValue($value)
  85. {
  86. $this->displayValue = $value;
  87. }
  88. /**
  89. * @return float
  90. */
  91. public function getDisplayValue()
  92. {
  93. if ($this->displayValue !== null) {
  94. return $this->displayValue;
  95. } else {
  96. return $this->getAmount()->getValue();
  97. }
  98. }
  99. /**
  100. * @return AmountInterface
  101. */
  102. public function getAmount()
  103. {
  104. return $this->amount;
  105. }
  106. /**
  107. * @return SaleableInterface
  108. */
  109. public function getSaleableItem()
  110. {
  111. return $this->saleableItem;
  112. }
  113. /**
  114. * @return \Magento\Framework\Pricing\Price\PriceInterface
  115. */
  116. public function getPrice()
  117. {
  118. return $this->price;
  119. }
  120. /**
  121. * @return string
  122. */
  123. public function getDisplayCurrencyCode()
  124. {
  125. return $this->priceCurrency->getCurrency()->getCurrencyCode();
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getDisplayCurrencySymbol()
  131. {
  132. return $this->priceCurrency->getCurrencySymbol();
  133. }
  134. /**
  135. * @return bool
  136. */
  137. public function hasAdjustmentsHtml()
  138. {
  139. return (bool) count($this->adjustmentsHtml);
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function getAdjustmentsHtml()
  145. {
  146. return implode('', $this->adjustmentsHtml);
  147. }
  148. /**
  149. * @return string
  150. */
  151. protected function _toHtml()
  152. {
  153. $adjustmentRenders = $this->getAdjustmentRenders();
  154. if ($adjustmentRenders) {
  155. $adjustmentHtml = $this->getAdjustments($adjustmentRenders);
  156. if (!$this->hasSkipAdjustments() ||
  157. ($this->hasSkipAdjustments() && $this->getSkipAdjustments() == false)) {
  158. $this->adjustmentsHtml = $adjustmentHtml;
  159. }
  160. }
  161. $html = parent::_toHtml();
  162. return $html;
  163. }
  164. /**
  165. * @return AdjustmentRenderInterface[]
  166. */
  167. protected function getAdjustmentRenders()
  168. {
  169. return $this->rendererPool->getAdjustmentRenders($this->saleableItem, $this->price);
  170. }
  171. /**
  172. * @param AdjustmentRenderInterface[] $adjustmentRenders
  173. * @return array
  174. */
  175. protected function getAdjustments($adjustmentRenders)
  176. {
  177. $this->setAdjustmentCssClasses($adjustmentRenders);
  178. $data = $this->getData();
  179. $adjustments = [];
  180. foreach ($adjustmentRenders as $adjustmentRender) {
  181. if ($this->getAmount()->getAdjustmentAmount($adjustmentRender->getAdjustmentCode()) !== false) {
  182. $html = $adjustmentRender->render($this, $data);
  183. if (trim($html)) {
  184. $adjustments[$adjustmentRender->getAdjustmentCode()] = $html;
  185. }
  186. }
  187. }
  188. return $adjustments;
  189. }
  190. /**
  191. * Format price value
  192. *
  193. * @param float $amount
  194. * @param bool $includeContainer
  195. * @param int $precision
  196. * @return float
  197. */
  198. public function formatCurrency(
  199. $amount,
  200. $includeContainer = true,
  201. $precision = PriceCurrencyInterface::DEFAULT_PRECISION
  202. ) {
  203. return $this->priceCurrency->format($amount, $includeContainer, $precision);
  204. }
  205. /**
  206. * @param AdjustmentRenderInterface[] $adjustmentRenders
  207. * @return array
  208. */
  209. protected function setAdjustmentCssClasses($adjustmentRenders)
  210. {
  211. $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
  212. $cssClasses = array_merge($cssClasses, array_keys($adjustmentRenders));
  213. $this->setData('adjustment_css_classes', join(' ', $cssClasses));
  214. return $this;
  215. }
  216. }