-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Configurable.php
458 lines (417 loc) · 13.3 KB
/
Configurable.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Swatches\Block\Product\Renderer;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Helper\Product as CatalogProduct;
use Magento\ConfigurableProduct\Helper\Data;
use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Stdlib\ArrayUtils;
use Magento\Store\Model\ScopeInterface;
use Magento\Swatches\Helper\Data as SwatchData;
use Magento\Swatches\Helper\Media;
use Magento\Swatches\Model\Swatch;
use Magento\Framework\App\ObjectManager;
use Magento\Swatches\Model\SwatchAttributesProvider;
/**
* Swatch renderer block
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable implements
\Magento\Framework\DataObject\IdentityInterface
{
/**
* Path to template file with Swatch renderer.
*/
const SWATCH_RENDERER_TEMPLATE = 'Magento_Swatches::product/view/renderer.phtml';
/**
* Path to default template file with standard Configurable renderer.
*/
const CONFIGURABLE_RENDERER_TEMPLATE = 'Magento_ConfigurableProduct::product/view/type/options/configurable.phtml';
/**
* Action name for ajax request
*/
const MEDIA_CALLBACK_ACTION = 'swatches/ajax/media';
/**
* @var Product
*/
protected $product;
/**
* @var SwatchData
*/
protected $swatchHelper;
/**
* @var Media
*/
protected $swatchMediaHelper;
/**
* Indicate if product has one or more Swatch attributes
*
* @deprecated unused
*
* @var boolean
*/
protected $isProductHasSwatchAttribute;
/**
* @var SwatchAttributesProvider
*/
private $swatchAttributesProvider;
/**
* @param Context $context
* @param ArrayUtils $arrayUtils
* @param EncoderInterface $jsonEncoder
* @param Data $helper
* @param CatalogProduct $catalogProduct
* @param CurrentCustomer $currentCustomer
* @param PriceCurrencyInterface $priceCurrency
* @param ConfigurableAttributeData $configurableAttributeData
* @param SwatchData $swatchHelper
* @param Media $swatchMediaHelper
* @param array $data other data
* @param SwatchAttributesProvider $swatchAttributesProvider
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Context $context,
ArrayUtils $arrayUtils,
EncoderInterface $jsonEncoder,
Data $helper,
CatalogProduct $catalogProduct,
CurrentCustomer $currentCustomer,
PriceCurrencyInterface $priceCurrency,
ConfigurableAttributeData $configurableAttributeData,
SwatchData $swatchHelper,
Media $swatchMediaHelper,
array $data = [],
SwatchAttributesProvider $swatchAttributesProvider = null
) {
$this->swatchHelper = $swatchHelper;
$this->swatchMediaHelper = $swatchMediaHelper;
$this->swatchAttributesProvider = $swatchAttributesProvider
?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class);
parent::__construct(
$context,
$arrayUtils,
$jsonEncoder,
$helper,
$catalogProduct,
$currentCustomer,
$priceCurrency,
$configurableAttributeData,
$data
);
}
/**
* Get Key for caching block content
*
* @return string
*/
public function getCacheKey()
{
return parent::getCacheKey() . '-' . $this->getProduct()->getId();
}
/**
* Get block cache life time
*
* @return int
*/
protected function getCacheLifetime()
{
return parent::hasCacheLifetime() ? parent::getCacheLifetime() : 3600;
}
/**
* Get Swatch config data
*
* @return string
*/
public function getJsonSwatchConfig()
{
$attributesData = $this->getSwatchAttributesData();
$allOptionIds = $this->getConfigurableOptionsIds($attributesData);
$swatchesData = $this->swatchHelper->getSwatchesByOptionsId($allOptionIds);
$config = [];
foreach ($attributesData as $attributeId => $attributeDataArray) {
if (isset($attributeDataArray['options'])) {
$config[$attributeId] = $this->addSwatchDataForAttribute(
$attributeDataArray['options'],
$swatchesData,
$attributeDataArray
);
}
}
return $this->jsonEncoder->encode($config);
}
/**
* Get number of swatches from config to show on product listing.
* Other swatches can be shown after click button 'Show more'
*
* @return string
*/
public function getNumberSwatchesPerProduct()
{
return $this->_scopeConfig->getValue(
'catalog/frontend/swatches_per_product',
ScopeInterface::SCOPE_STORE
);
}
/**
* Set product to block
*
* @param Product $product
* @return $this
*/
public function setProduct(Product $product)
{
$this->product = $product;
return $this;
}
/**
* Override parent function
*
* @return Product
*/
public function getProduct()
{
if (!$this->product) {
$this->product = parent::getProduct();
}
return $this->product;
}
/**
* @return array
*/
protected function getSwatchAttributesData()
{
$swatchAttributes = [];
try {
$swatchAttributes = $this->swatchHelper->getSwatchAttributesAsArray($this->getProduct());
} catch (LocalizedException $e) {
$this->_logger->critical("Cannot get swatch attributes data\n" . $e->getMessage());
}
return $swatchAttributes;
}
/**
* @deprecated unused
* @see isProductHasSwatchAttribute().
*
* @codeCoverageIgnore
* @return void
*/
protected function initIsProductHasSwatchAttribute()
{
try {
$this->isProductHasSwatchAttribute = $this->swatchHelper->isProductHasSwatch($this->getProduct());
} catch (LocalizedException $e) {
$this->_logger->critical("Cannot check if product has swatch\n" . $e->getMessage());
}
}
/**
* @codeCoverageIgnore
* @return bool
*/
protected function isProductHasSwatchAttribute()
{
$swatchAttributes = $this->swatchAttributesProvider->provide($this->getProduct());
return count($swatchAttributes) > 0;
}
/**
* Add Swatch Data for attribute
*
* @param array $options
* @param array $swatchesCollectionArray
* @param array $attributeDataArray
* @return array
*/
protected function addSwatchDataForAttribute(
array $options,
array $swatchesCollectionArray,
array $attributeDataArray
) {
$result = [];
foreach ($options as $optionId => $label) {
if (isset($swatchesCollectionArray[$optionId])) {
$result[$optionId] = $this->extractNecessarySwatchData($swatchesCollectionArray[$optionId]);
$result[$optionId] = $this->addAdditionalMediaData($result[$optionId], $optionId, $attributeDataArray);
$result[$optionId]['label'] = $label;
}
}
return $result;
}
/**
* Add media from variation
*
* @param array $swatch
* @param integer $optionId
* @param array $attributeDataArray
* @return array
*/
protected function addAdditionalMediaData(array $swatch, $optionId, array $attributeDataArray)
{
if (
isset($attributeDataArray['use_product_image_for_swatch'])
&& $attributeDataArray['use_product_image_for_swatch']
) {
$variationMedia = $this->getVariationMedia($attributeDataArray['attribute_code'], $optionId);
if (! empty($variationMedia)) {
$swatch['type'] = Swatch::SWATCH_TYPE_VISUAL_IMAGE;
$swatch = array_merge($swatch, $variationMedia);
}
}
return $swatch;
}
/**
* Retrieve Swatch data for config
*
* @param array $swatchDataArray
* @return array
*/
protected function extractNecessarySwatchData(array $swatchDataArray)
{
$result['type'] = $swatchDataArray['type'];
if ($result['type'] == Swatch::SWATCH_TYPE_VISUAL_IMAGE && !empty($swatchDataArray['value'])) {
$result['value'] = $this->swatchMediaHelper->getSwatchAttributeImage(
Swatch::SWATCH_IMAGE_NAME,
$swatchDataArray['value']
);
$result['thumb'] = $this->swatchMediaHelper->getSwatchAttributeImage(
Swatch::SWATCH_THUMBNAIL_NAME,
$swatchDataArray['value']
);
} else {
$result['value'] = $swatchDataArray['value'];
}
return $result;
}
/**
* Generate Product Media array
*
* @param string $attributeCode
* @param integer $optionId
* @return array
*/
protected function getVariationMedia($attributeCode, $optionId)
{
$variationProduct = $this->swatchHelper->loadFirstVariationWithSwatchImage(
$this->getProduct(),
[$attributeCode => $optionId]
);
if (!$variationProduct) {
$variationProduct = $this->swatchHelper->loadFirstVariationWithImage(
$this->getProduct(),
[$attributeCode => $optionId]
);
}
$variationMediaArray = [];
if ($variationProduct) {
$variationMediaArray = [
'value' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_IMAGE_NAME),
'thumb' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_THUMBNAIL_NAME),
];
}
return $variationMediaArray;
}
/**
* @param Product $childProduct
* @param string $imageType
* @return string
*/
protected function getSwatchProductImage(Product $childProduct, $imageType)
{
if ($this->isProductHasImage($childProduct, Swatch::SWATCH_IMAGE_NAME)) {
$swatchImageId = $imageType;
$imageAttributes = ['type' => Swatch::SWATCH_IMAGE_NAME];
} elseif ($this->isProductHasImage($childProduct, 'image')) {
$swatchImageId = $imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base' : 'swatch_thumb_base';
$imageAttributes = ['type' => 'image'];
}
if (isset($swatchImageId)) {
return $this->_imageHelper->init($childProduct, $swatchImageId, $imageAttributes)->getUrl();
}
}
/**
* @param Product $product
* @param string $imageType
* @return bool
*/
protected function isProductHasImage(Product $product, $imageType)
{
return $product->getData($imageType) !== null && $product->getData($imageType) != SwatchData::EMPTY_IMAGE_VALUE;
}
/**
* @param array $attributeData
* @return array
*/
protected function getConfigurableOptionsIds(array $attributeData)
{
$ids = [];
foreach ($this->getAllowProducts() as $product) {
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute */
foreach ($this->helper->getAllowAttributes($this->getProduct()) as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
if (isset($attributeData[$productAttributeId])) {
$ids[$product->getData($productAttribute->getAttributeCode())] = 1;
}
}
}
return array_keys($ids);
}
/**
* Produce and return block's html output.
*
* @return string
*/
protected function _toHtml()
{
$this->setTemplate(
$this->getRendererTemplate()
);
return parent::_toHtml();
}
/**
* @codeCoverageIgnore
* @return string
*/
protected function getRendererTemplate()
{
return $this->isProductHasSwatchAttribute() ?
self::SWATCH_RENDERER_TEMPLATE : self::CONFIGURABLE_RENDERER_TEMPLATE;
}
/**
* @deprecated
* @codeCoverageIgnore
* @return string
*/
protected function getHtmlOutput()
{
return parent::_toHtml();
}
/**
* @return string
*/
public function getMediaCallback()
{
return $this->getUrl(self::MEDIA_CALLBACK_ACTION, ['_secure' => $this->getRequest()->isSecure()]);
}
/**
* Return unique ID(s) for each object in system
*
* @return string[]
*/
public function getIdentities()
{
if ($this->product instanceof \Magento\Framework\DataObject\IdentityInterface) {
return $this->product->getIdentities();
} else {
return [];
}
}
}