/
Bundle.php
354 lines (315 loc) · 10.9 KB
/
Bundle.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Bundle\Block\Catalog\Product\View\Type;
use Magento\Bundle\Model\Option;
use Magento\Catalog\Model\Product;
/**
* Catalog bundle product info block
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Bundle extends \Magento\Catalog\Block\Product\View\AbstractView
{
/**
* @var array
*/
protected $options;
/**
* Catalog product
*
* @var \Magento\Catalog\Helper\Product
*/
protected $catalogProduct;
/**
* @var \Magento\Bundle\Model\Product\PriceFactory
*/
protected $productPriceFactory;
/**
* @var \Magento\Framework\Json\EncoderInterface
*/
protected $jsonEncoder;
/**
* @var \Magento\Framework\Locale\FormatInterface
*/
protected $localeFormat;
/**
* @var array
*/
private $selectedOptions = [];
/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
* @param \Magento\Catalog\Helper\Product $catalogProduct
* @param \Magento\Bundle\Model\Product\PriceFactory $productPrice
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
\Magento\Catalog\Helper\Product $catalogProduct,
\Magento\Bundle\Model\Product\PriceFactory $productPrice,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Locale\FormatInterface $localeFormat,
array $data = []
) {
$this->catalogProduct = $catalogProduct;
$this->productPriceFactory = $productPrice;
$this->jsonEncoder = $jsonEncoder;
$this->localeFormat = $localeFormat;
parent::__construct(
$context,
$arrayUtils,
$data
);
}
/**
* @return array
*/
public function getOptions()
{
if (!$this->options) {
$product = $this->getProduct();
$typeInstance = $product->getTypeInstance();
$typeInstance->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $typeInstance->getOptionsCollection($product);
$selectionCollection = $typeInstance->getSelectionsCollection(
$typeInstance->getOptionsIds($product),
$product
);
$this->options = $optionCollection->appendSelections(
$selectionCollection,
false,
$this->catalogProduct->getSkipSaleableCheck()
);
}
return $this->options;
}
/**
* @return bool
*/
public function hasOptions()
{
$this->getOptions();
if (empty($this->options) || !$this->getProduct()->isSalable()) {
return false;
}
return true;
}
/**
* Returns JSON encoded config to be used in JS scripts
*
* @return string
*
*/
public function getJsonConfig()
{
/** @var Option[] $optionsArray */
$optionsArray = $this->getOptions();
$options = [];
$currentProduct = $this->getProduct();
$defaultValues = [];
$preConfiguredFlag = $currentProduct->hasPreconfiguredValues();
/** @var \Magento\Framework\DataObject|null $preConfiguredValues */
$preConfiguredValues = $preConfiguredFlag ? $currentProduct->getPreconfiguredValues() : null;
$position = 0;
foreach ($optionsArray as $optionItem) {
/* @var $optionItem Option */
if (!$optionItem->getSelections()) {
continue;
}
$optionId = $optionItem->getId();
$options[$optionId] = $this->getOptionItemData($optionItem, $currentProduct, $position);
// Add attribute default value (if set)
if ($preConfiguredFlag) {
$configValue = $preConfiguredValues->getData('bundle_option/' . $optionId);
if ($configValue) {
$defaultValues[$optionId] = $configValue;
}
}
$position++;
}
$config = $this->getConfigData($currentProduct, $options);
$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();
if ($preConfiguredFlag && !empty($defaultValues)) {
$config['defaultValues'] = $defaultValues;
}
return $this->jsonEncoder->encode($config);
}
/**
* Get html for option
*
* @param Option $option
* @return string
*/
public function getOptionHtml(Option $option)
{
$optionBlock = $this->getChildBlock($option->getType());
if (!$optionBlock) {
return __('There is no defined renderer for "%1" option type.', $option->getType());
}
return $optionBlock->setOption($option)->toHtml();
}
/**
* Get formed data from option selection item
*
* @param Product $product
* @param Product $selection
* @return array
*/
private function getSelectionItemData(Product $product, Product $selection)
{
$qty = ($selection->getSelectionQty() * 1) ?: '1';
$optionPriceAmount = $product->getPriceInfo()
->getPrice('bundle_option')
->getOptionSelectionAmount($selection);
$finalPrice = $optionPriceAmount->getValue();
$basePrice = $optionPriceAmount->getBaseAmount();
$selection = [
'qty' => $qty,
'customQty' => $selection->getSelectionCanChangeQty(),
'optionId' => $selection->getId(),
'prices' => [
'oldPrice' => [
'amount' => $basePrice
],
'basePrice' => [
'amount' => $basePrice
],
'finalPrice' => [
'amount' => $finalPrice
]
],
'priceType' => $selection->getSelectionPriceType(),
'tierPrice' => $this->getTierPrices($product, $selection),
'name' => $selection->getName(),
'canApplyMsrp' => false
];
return $selection;
}
/**
* Get tier prices from option selection item
*
* @param Product $product
* @param Product $selection
* @return array
*/
private function getTierPrices(Product $product, Product $selection)
{
// recalculate currency
$tierPrices = $selection->getPriceInfo()
->getPrice(\Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE)
->getTierPriceList();
foreach ($tierPrices as &$tierPriceInfo) {
/** @var \Magento\Framework\Pricing\Amount\Base $price */
$price = $tierPriceInfo['price'];
$priceBaseAmount = $price->getBaseAmount();
$priceValue = $price->getValue();
$bundleProductPrice = $this->productPriceFactory->create();
$priceBaseAmount = $bundleProductPrice->getLowestPrice($product, $priceBaseAmount);
$priceValue = $bundleProductPrice->getLowestPrice($product, $priceValue);
$tierPriceInfo['prices'] = [
'oldPrice' => [
'amount' => $priceBaseAmount
],
'basePrice' => [
'amount' => $priceBaseAmount
],
'finalPrice' => [
'amount' => $priceValue
]
];
}
return $tierPrices;
}
/**
* Get formed data from selections of option
*
* @param Option $option
* @param Product $product
* @return array
*/
private function getSelections(Option $option, Product $product)
{
$selections = [];
$selectionCount = count($option->getSelections());
foreach ($option->getSelections() as $selectionItem) {
/* @var $selectionItem Product */
$selectionId = $selectionItem->getSelectionId();
$selections[$selectionId] = $this->getSelectionItemData($product, $selectionItem);
if (($selectionItem->getIsDefault() || $selectionCount == 1 && $option->getRequired())
&& $selectionItem->isSalable()
) {
$this->selectedOptions[$option->getId()][] = $selectionId;
}
}
return $selections;
}
/**
* Get formed data from option
*
* @param Option $option
* @param Product $product
* @param int $position
* @return array
*/
private function getOptionItemData(Option $option, Product $product, $position)
{
return [
'selections' => $this->getSelections($option, $product),
'title' => $option->getTitle(),
'isMulti' => in_array($option->getType(), ['multi', 'checkbox']),
'position' => $position
];
}
/**
* Get formed config data from calculated options data
*
* @param Product $product
* @param array $options
* @return array
*/
private function getConfigData(Product $product, array $options)
{
$isFixedPrice = $this->getProduct()->getPriceType() == \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED;
$productAmount = $product
->getPriceInfo()
->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)
->getPriceWithoutOption();
$baseProductAmount = $product
->getPriceInfo()
->getPrice(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)
->getAmount();
$config = [
'options' => $options,
'selected' => $this->selectedOptions,
'bundleId' => $product->getId(),
'priceFormat' => $this->localeFormat->getPriceFormat(),
'prices' => [
'oldPrice' => [
'amount' => $isFixedPrice ? $baseProductAmount->getValue() : 0
],
'basePrice' => [
'amount' => $isFixedPrice ? $productAmount->getBaseAmount() : 0
],
'finalPrice' => [
'amount' => $isFixedPrice ? $productAmount->getValue() : 0
]
],
'priceType' => $product->getPriceType(),
'isFixedPrice' => $isFixedPrice,
];
return $config;
}
}