/
Options.php
265 lines (239 loc) · 7.18 KB
/
Options.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Product options block
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Catalog\Block\Product\View;
use Magento\Catalog\Model\Product;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Options extends \Magento\Framework\View\Element\Template
{
/**
* @var Product
*/
protected $_product;
/**
* Product option
*
* @var \Magento\Catalog\Model\Product\Option
*/
protected $_option;
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_registry = null;
/**
* Catalog product
*
* @var Product
*/
protected $_catalogProduct;
/**
* @var \Magento\Framework\Json\EncoderInterface
*/
protected $_jsonEncoder;
/**
* @var \Magento\Framework\Pricing\Helper\Data
*/
protected $pricingHelper;
/**
* @var \Magento\Catalog\Helper\Data
*/
protected $_catalogData;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
* @param \Magento\Catalog\Helper\Data $catalogData
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Catalog\Model\Product\Option $option
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
\Magento\Catalog\Helper\Data $catalogData,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Catalog\Model\Product\Option $option,
\Magento\Framework\Registry $registry,
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
array $data = []
) {
$this->pricingHelper = $pricingHelper;
$this->_catalogData = $catalogData;
$this->_jsonEncoder = $jsonEncoder;
$this->_registry = $registry;
$this->_option = $option;
$this->arrayUtils = $arrayUtils;
parent::__construct($context, $data);
}
/**
* Retrieve product object
*
* @return Product
* @throws \LogicExceptions
*/
public function getProduct()
{
if (!$this->_product) {
if ($this->_registry->registry('current_product')) {
$this->_product = $this->_registry->registry('current_product');
} else {
throw new \LogicException('Product is not defined');
}
}
return $this->_product;
}
/**
* Set product object
*
* @param Product $product
* @return \Magento\Catalog\Block\Product\View\Options
*/
public function setProduct(Product $product = null)
{
$this->_product = $product;
return $this;
}
/**
* @param string $type
* @return string
*/
public function getGroupOfOption($type)
{
$group = $this->_option->getGroupByType($type);
return $group == '' ? 'default' : $group;
}
/**
* Get product options
*
* @return array
*/
public function getOptions()
{
return $this->getProduct()->getOptions();
}
/**
* @return bool
*/
public function hasOptions()
{
if ($this->getOptions()) {
return true;
}
return false;
}
/**
* Get price configuration
*
* @param \Magento\Catalog\Model\Product\Option\Value|\Magento\Catalog\Model\Product\Option $option
* @return array
*/
protected function _getPriceConfiguration($option)
{
$optionPrice = $this->pricingHelper->currency($option->getPrice(true), false, false);
$data = [
'prices' => [
'oldPrice' => [
'amount' => $this->pricingHelper->currency($option->getRegularPrice(), false, false),
'adjustments' => [],
],
'basePrice' => [
'amount' => $this->_catalogData->getTaxPrice(
$option->getProduct(),
$optionPrice,
false,
null,
null,
null,
null,
null,
false
),
],
'finalPrice' => [
'amount' => $this->_catalogData->getTaxPrice(
$option->getProduct(),
$optionPrice,
true,
null,
null,
null,
null,
null,
false
),
],
],
'type' => $option->getPriceType(),
'name' => $option->getTitle()
];
return $data;
}
/**
* Get json representation of
*
* @return string
*/
public function getJsonConfig()
{
$config = [];
foreach ($this->getOptions() as $option) {
/* @var $option \Magento\Catalog\Model\Product\Option */
if ($option->getGroupByType() == \Magento\Catalog\Model\Product\Option::OPTION_GROUP_SELECT) {
$tmpPriceValues = [];
foreach ($option->getValues() as $valueId => $value) {
$tmpPriceValues[$valueId] = $this->_getPriceConfiguration($value);
}
$priceValue = $tmpPriceValues;
} else {
$priceValue = $this->_getPriceConfiguration($option);
}
$config[$option->getId()] = $priceValue;
}
$configObj = new \Magento\Framework\DataObject(
[
'config' => $config,
]
);
//pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
$this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);
$config=$configObj->getConfig();
return $this->_jsonEncoder->encode($config);
}
/**
* Get option html block
*
* @param \Magento\Catalog\Model\Product\Option $option
* @return string
*/
public function getOptionHtml(\Magento\Catalog\Model\Product\Option $option)
{
$type = $this->getGroupOfOption($option->getType());
$renderer = $this->getChildBlock($type);
$renderer->setProduct($this->getProduct())->setOption($option);
return $this->getChildHtml($type, false);
}
/**
* Decorate a plain array of arrays or objects
*
* @param array $array
* @param string $prefix
* @param bool $forceSetAll
* @return array
*/
public function decorateArray($array, $prefix = 'decorated_', $forceSetAll = false)
{
return $this->arrayUtils->decorateArray($array, $prefix, $forceSetAll);
}
}