3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types = 1 );
6
7
namespace Magento \Swatches \Block \Product \Renderer ;
7
8
8
9
use Magento \Catalog \Block \Product \Context ;
@@ -57,6 +58,16 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
57
58
*/
58
59
const SWATCH_THUMBNAIL_NAME = 'swatchThumb ' ;
59
60
61
+ /**
62
+ * Config path which contains number of swatches per product
63
+ */
64
+ const XML_PATH_SWATCHES_PER_PRODUCT = 'catalog/frontend/swatches_per_product ' ;
65
+
66
+ /**
67
+ * Config path if swatch tooltips are enabled
68
+ */
69
+ const XML_PATH_SHOW_SWATCH_TOOLTIP = 'catalog/frontend/show_swatch_tooltip ' ;
70
+
60
71
/**
61
72
* @var Product
62
73
*/
@@ -93,19 +104,19 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
93
104
94
105
/**
95
106
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
96
- * @param Context $context
97
- * @param ArrayUtils $arrayUtils
98
- * @param EncoderInterface $jsonEncoder
99
- * @param Data $helper
100
- * @param CatalogProduct $catalogProduct
101
- * @param CurrentCustomer $currentCustomer
102
- * @param PriceCurrencyInterface $priceCurrency
103
- * @param ConfigurableAttributeData $configurableAttributeData
104
- * @param SwatchData $swatchHelper
105
- * @param Media $swatchMediaHelper
106
- * @param array $data
107
+ * @param Context $context
108
+ * @param ArrayUtils $arrayUtils
109
+ * @param EncoderInterface $jsonEncoder
110
+ * @param Data $helper
111
+ * @param CatalogProduct $catalogProduct
112
+ * @param CurrentCustomer $currentCustomer
113
+ * @param PriceCurrencyInterface $priceCurrency
114
+ * @param ConfigurableAttributeData $configurableAttributeData
115
+ * @param SwatchData $swatchHelper
116
+ * @param Media $swatchMediaHelper
117
+ * @param array $data
107
118
* @param SwatchAttributesProvider|null $swatchAttributesProvider
108
- * @param UrlBuilder|null $imageUrlBuilder
119
+ * @param UrlBuilder|null $imageUrlBuilder
109
120
*/
110
121
public function __construct (
111
122
Context $ context ,
@@ -172,7 +183,6 @@ public function getJsonSwatchConfig()
172
183
$ attributesData = $ this ->getSwatchAttributesData ();
173
184
$ allOptionIds = $ this ->getConfigurableOptionsIds ($ attributesData );
174
185
$ swatchesData = $ this ->swatchHelper ->getSwatchesByOptionsId ($ allOptionIds );
175
-
176
186
$ config = [];
177
187
foreach ($ attributesData as $ attributeId => $ attributeDataArray ) {
178
188
if (isset ($ attributeDataArray ['options ' ])) {
@@ -200,7 +210,20 @@ public function getJsonSwatchConfig()
200
210
public function getNumberSwatchesPerProduct ()
201
211
{
202
212
return $ this ->_scopeConfig ->getValue (
203
- 'catalog/frontend/swatches_per_product ' ,
213
+ self ::XML_PATH_SWATCHES_PER_PRODUCT ,
214
+ ScopeInterface::SCOPE_STORE
215
+ );
216
+ }
217
+
218
+ /**
219
+ * Get config if swatch tooltips should be rendered.
220
+ *
221
+ * @return string
222
+ */
223
+ public function getShowSwatchTooltip ()
224
+ {
225
+ return $ this ->_scopeConfig ->getValue (
226
+ self ::XML_PATH_SHOW_SWATCH_TOOLTIP ,
204
227
ScopeInterface::SCOPE_STORE
205
228
);
206
229
}
@@ -209,11 +232,13 @@ public function getNumberSwatchesPerProduct()
209
232
* Set product to block
210
233
*
211
234
* @param Product $product
235
+ *
212
236
* @return $this
213
237
*/
214
238
public function setProduct (Product $ product )
215
239
{
216
240
$ this ->product = $ product ;
241
+
217
242
return $ this ;
218
243
}
219
244
@@ -244,10 +269,10 @@ protected function getSwatchAttributesData()
244
269
/**
245
270
* Init isProductHasSwatchAttribute.
246
271
*
272
+ * @return void
247
273
* @deprecated 100.1.5 Method isProductHasSwatchAttribute() is used instead of this.
248
274
*
249
275
* @codeCoverageIgnore
250
- * @return void
251
276
*/
252
277
protected function initIsProductHasSwatchAttribute ()
253
278
{
@@ -263,6 +288,7 @@ protected function initIsProductHasSwatchAttribute()
263
288
protected function isProductHasSwatchAttribute ()
264
289
{
265
290
$ swatchAttributes = $ this ->swatchAttributesProvider ->provide ($ this ->getProduct ());
291
+
266
292
return count ($ swatchAttributes ) > 0 ;
267
293
}
268
294
@@ -272,6 +298,7 @@ protected function isProductHasSwatchAttribute()
272
298
* @param array $options
273
299
* @param array $swatchesCollectionArray
274
300
* @param array $attributeDataArray
301
+ *
275
302
* @return array
276
303
*/
277
304
protected function addSwatchDataForAttribute (
@@ -294,9 +321,10 @@ protected function addSwatchDataForAttribute(
294
321
/**
295
322
* Add media from variation
296
323
*
297
- * @param array $swatch
324
+ * @param array $swatch
298
325
* @param integer $optionId
299
- * @param array $attributeDataArray
326
+ * @param array $attributeDataArray
327
+ *
300
328
* @return array
301
329
*/
302
330
protected function addAdditionalMediaData (array $ swatch , $ optionId , array $ attributeDataArray )
@@ -305,24 +333,25 @@ protected function addAdditionalMediaData(array $swatch, $optionId, array $attri
305
333
&& $ attributeDataArray ['use_product_image_for_swatch ' ]
306
334
) {
307
335
$ variationMedia = $ this ->getVariationMedia ($ attributeDataArray ['attribute_code ' ], $ optionId );
308
- if (! empty ($ variationMedia )) {
336
+ if (!empty ($ variationMedia )) {
309
337
$ swatch ['type ' ] = Swatch::SWATCH_TYPE_VISUAL_IMAGE ;
310
338
$ swatch = array_merge ($ swatch , $ variationMedia );
311
339
}
312
340
}
341
+
313
342
return $ swatch ;
314
343
}
315
344
316
345
/**
317
346
* Retrieve Swatch data for config
318
347
*
319
348
* @param array $swatchDataArray
349
+ *
320
350
* @return array
321
351
*/
322
352
protected function extractNecessarySwatchData (array $ swatchDataArray )
323
353
{
324
354
$ result ['type ' ] = $ swatchDataArray ['type ' ];
325
-
326
355
if ($ result ['type ' ] == Swatch::SWATCH_TYPE_VISUAL_IMAGE && !empty ($ swatchDataArray ['value ' ])) {
327
356
$ result ['value ' ] = $ this ->swatchMediaHelper ->getSwatchAttributeImage (
328
357
Swatch::SWATCH_IMAGE_NAME ,
@@ -342,8 +371,9 @@ protected function extractNecessarySwatchData(array $swatchDataArray)
342
371
/**
343
372
* Generate Product Media array
344
373
*
345
- * @param string $attributeCode
374
+ * @param string $attributeCode
346
375
* @param integer $optionId
376
+ *
347
377
* @return array
348
378
*/
349
379
protected function getVariationMedia ($ attributeCode , $ optionId )
@@ -352,14 +382,12 @@ protected function getVariationMedia($attributeCode, $optionId)
352
382
$ this ->getProduct (),
353
383
[$ attributeCode => $ optionId ]
354
384
);
355
-
356
385
if (!$ variationProduct ) {
357
386
$ variationProduct = $ this ->swatchHelper ->loadFirstVariationWithImage (
358
387
$ this ->getProduct (),
359
388
[$ attributeCode => $ optionId ]
360
389
);
361
390
}
362
-
363
391
$ variationMediaArray = [];
364
392
if ($ variationProduct ) {
365
393
$ variationMediaArray = [
@@ -375,7 +403,8 @@ protected function getVariationMedia($attributeCode, $optionId)
375
403
* Get swatch product image.
376
404
*
377
405
* @param Product $childProduct
378
- * @param string $imageType
406
+ * @param string $imageType
407
+ *
379
408
* @return string
380
409
*/
381
410
protected function getSwatchProductImage (Product $ childProduct , $ imageType )
@@ -387,7 +416,6 @@ protected function getSwatchProductImage(Product $childProduct, $imageType)
387
416
$ swatchImageId = $ imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base ' : 'swatch_thumb_base ' ;
388
417
$ imageAttributes = ['type ' => 'image ' ];
389
418
}
390
-
391
419
if (!empty ($ swatchImageId ) && !empty ($ imageAttributes ['type ' ])) {
392
420
return $ this ->imageUrlBuilder ->getUrl ($ childProduct ->getData ($ imageAttributes ['type ' ]), $ swatchImageId );
393
421
}
@@ -397,7 +425,8 @@ protected function getSwatchProductImage(Product $childProduct, $imageType)
397
425
* Check if product have image.
398
426
*
399
427
* @param Product $product
400
- * @param string $imageType
428
+ * @param string $imageType
429
+ *
401
430
* @return bool
402
431
*/
403
432
protected function isProductHasImage (Product $ product , $ imageType )
@@ -409,6 +438,7 @@ protected function isProductHasImage(Product $product, $imageType)
409
438
* Get configurable options ids.
410
439
*
411
440
* @param array $attributeData
441
+ *
412
442
* @return array
413
443
* @since 100.0.3
414
444
*/
@@ -425,6 +455,7 @@ protected function getConfigurableOptionsIds(array $attributeData)
425
455
}
426
456
}
427
457
}
458
+
428
459
return array_keys ($ ids );
429
460
}
430
461
@@ -509,7 +540,6 @@ public function getJsonSwatchSizeConfig()
509
540
{
510
541
$ imageConfig = $ this ->swatchMediaHelper ->getImageConfig ();
511
542
$ sizeConfig = [];
512
-
513
543
$ sizeConfig [self ::SWATCH_IMAGE_NAME ]['width ' ] = $ imageConfig [Swatch::SWATCH_IMAGE_NAME ]['width ' ];
514
544
$ sizeConfig [self ::SWATCH_IMAGE_NAME ]['height ' ] = $ imageConfig [Swatch::SWATCH_IMAGE_NAME ]['height ' ];
515
545
$ sizeConfig [self ::SWATCH_THUMBNAIL_NAME ]['height ' ] = $ imageConfig [Swatch::SWATCH_THUMBNAIL_NAME ]['height ' ];
0 commit comments