/
Amount.php
241 lines (216 loc) · 5.79 KB
/
Amount.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?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;
}
}