Skip to content

Commit dbc514c

Browse files
committed
add config for disabling swatch tooltips
1 parent e56640c commit dbc514c

File tree

6 files changed

+74
-34
lines changed

6 files changed

+74
-34
lines changed

app/code/Magento/Swatches/Block/Product/Renderer/Configurable.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types = 1);
67
namespace Magento\Swatches\Block\Product\Renderer;
78

89
use Magento\Catalog\Block\Product\Context;
@@ -57,6 +58,16 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
5758
*/
5859
const SWATCH_THUMBNAIL_NAME = 'swatchThumb';
5960

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+
6071
/**
6172
* @var Product
6273
*/
@@ -93,19 +104,19 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
93104

94105
/**
95106
* @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
107118
* @param SwatchAttributesProvider|null $swatchAttributesProvider
108-
* @param UrlBuilder|null $imageUrlBuilder
119+
* @param UrlBuilder|null $imageUrlBuilder
109120
*/
110121
public function __construct(
111122
Context $context,
@@ -172,7 +183,6 @@ public function getJsonSwatchConfig()
172183
$attributesData = $this->getSwatchAttributesData();
173184
$allOptionIds = $this->getConfigurableOptionsIds($attributesData);
174185
$swatchesData = $this->swatchHelper->getSwatchesByOptionsId($allOptionIds);
175-
176186
$config = [];
177187
foreach ($attributesData as $attributeId => $attributeDataArray) {
178188
if (isset($attributeDataArray['options'])) {
@@ -200,7 +210,20 @@ public function getJsonSwatchConfig()
200210
public function getNumberSwatchesPerProduct()
201211
{
202212
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,
204227
ScopeInterface::SCOPE_STORE
205228
);
206229
}
@@ -209,11 +232,13 @@ public function getNumberSwatchesPerProduct()
209232
* Set product to block
210233
*
211234
* @param Product $product
235+
*
212236
* @return $this
213237
*/
214238
public function setProduct(Product $product)
215239
{
216240
$this->product = $product;
241+
217242
return $this;
218243
}
219244

@@ -244,10 +269,10 @@ protected function getSwatchAttributesData()
244269
/**
245270
* Init isProductHasSwatchAttribute.
246271
*
272+
* @return void
247273
* @deprecated 100.1.5 Method isProductHasSwatchAttribute() is used instead of this.
248274
*
249275
* @codeCoverageIgnore
250-
* @return void
251276
*/
252277
protected function initIsProductHasSwatchAttribute()
253278
{
@@ -263,6 +288,7 @@ protected function initIsProductHasSwatchAttribute()
263288
protected function isProductHasSwatchAttribute()
264289
{
265290
$swatchAttributes = $this->swatchAttributesProvider->provide($this->getProduct());
291+
266292
return count($swatchAttributes) > 0;
267293
}
268294

@@ -272,6 +298,7 @@ protected function isProductHasSwatchAttribute()
272298
* @param array $options
273299
* @param array $swatchesCollectionArray
274300
* @param array $attributeDataArray
301+
*
275302
* @return array
276303
*/
277304
protected function addSwatchDataForAttribute(
@@ -294,9 +321,10 @@ protected function addSwatchDataForAttribute(
294321
/**
295322
* Add media from variation
296323
*
297-
* @param array $swatch
324+
* @param array $swatch
298325
* @param integer $optionId
299-
* @param array $attributeDataArray
326+
* @param array $attributeDataArray
327+
*
300328
* @return array
301329
*/
302330
protected function addAdditionalMediaData(array $swatch, $optionId, array $attributeDataArray)
@@ -305,24 +333,25 @@ protected function addAdditionalMediaData(array $swatch, $optionId, array $attri
305333
&& $attributeDataArray['use_product_image_for_swatch']
306334
) {
307335
$variationMedia = $this->getVariationMedia($attributeDataArray['attribute_code'], $optionId);
308-
if (! empty($variationMedia)) {
336+
if (!empty($variationMedia)) {
309337
$swatch['type'] = Swatch::SWATCH_TYPE_VISUAL_IMAGE;
310338
$swatch = array_merge($swatch, $variationMedia);
311339
}
312340
}
341+
313342
return $swatch;
314343
}
315344

316345
/**
317346
* Retrieve Swatch data for config
318347
*
319348
* @param array $swatchDataArray
349+
*
320350
* @return array
321351
*/
322352
protected function extractNecessarySwatchData(array $swatchDataArray)
323353
{
324354
$result['type'] = $swatchDataArray['type'];
325-
326355
if ($result['type'] == Swatch::SWATCH_TYPE_VISUAL_IMAGE && !empty($swatchDataArray['value'])) {
327356
$result['value'] = $this->swatchMediaHelper->getSwatchAttributeImage(
328357
Swatch::SWATCH_IMAGE_NAME,
@@ -342,8 +371,9 @@ protected function extractNecessarySwatchData(array $swatchDataArray)
342371
/**
343372
* Generate Product Media array
344373
*
345-
* @param string $attributeCode
374+
* @param string $attributeCode
346375
* @param integer $optionId
376+
*
347377
* @return array
348378
*/
349379
protected function getVariationMedia($attributeCode, $optionId)
@@ -352,14 +382,12 @@ protected function getVariationMedia($attributeCode, $optionId)
352382
$this->getProduct(),
353383
[$attributeCode => $optionId]
354384
);
355-
356385
if (!$variationProduct) {
357386
$variationProduct = $this->swatchHelper->loadFirstVariationWithImage(
358387
$this->getProduct(),
359388
[$attributeCode => $optionId]
360389
);
361390
}
362-
363391
$variationMediaArray = [];
364392
if ($variationProduct) {
365393
$variationMediaArray = [
@@ -375,7 +403,8 @@ protected function getVariationMedia($attributeCode, $optionId)
375403
* Get swatch product image.
376404
*
377405
* @param Product $childProduct
378-
* @param string $imageType
406+
* @param string $imageType
407+
*
379408
* @return string
380409
*/
381410
protected function getSwatchProductImage(Product $childProduct, $imageType)
@@ -387,7 +416,6 @@ protected function getSwatchProductImage(Product $childProduct, $imageType)
387416
$swatchImageId = $imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base' : 'swatch_thumb_base';
388417
$imageAttributes = ['type' => 'image'];
389418
}
390-
391419
if (!empty($swatchImageId) && !empty($imageAttributes['type'])) {
392420
return $this->imageUrlBuilder->getUrl($childProduct->getData($imageAttributes['type']), $swatchImageId);
393421
}
@@ -397,7 +425,8 @@ protected function getSwatchProductImage(Product $childProduct, $imageType)
397425
* Check if product have image.
398426
*
399427
* @param Product $product
400-
* @param string $imageType
428+
* @param string $imageType
429+
*
401430
* @return bool
402431
*/
403432
protected function isProductHasImage(Product $product, $imageType)
@@ -409,6 +438,7 @@ protected function isProductHasImage(Product $product, $imageType)
409438
* Get configurable options ids.
410439
*
411440
* @param array $attributeData
441+
*
412442
* @return array
413443
* @since 100.0.3
414444
*/
@@ -425,6 +455,7 @@ protected function getConfigurableOptionsIds(array $attributeData)
425455
}
426456
}
427457
}
458+
428459
return array_keys($ids);
429460
}
430461

@@ -509,7 +540,6 @@ public function getJsonSwatchSizeConfig()
509540
{
510541
$imageConfig = $this->swatchMediaHelper->getImageConfig();
511542
$sizeConfig = [];
512-
513543
$sizeConfig[self::SWATCH_IMAGE_NAME]['width'] = $imageConfig[Swatch::SWATCH_IMAGE_NAME]['width'];
514544
$sizeConfig[self::SWATCH_IMAGE_NAME]['height'] = $imageConfig[Swatch::SWATCH_IMAGE_NAME]['height'];
515545
$sizeConfig[self::SWATCH_THUMBNAIL_NAME]['height'] = $imageConfig[Swatch::SWATCH_THUMBNAIL_NAME]['height'];

app/code/Magento/Swatches/etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<label>Show Swatches in Product List</label>
1818
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1919
</field>
20+
<field id="show_swatch_tooltip" translate="label" type="select" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
21+
<label>Show Swatch Tooltip</label>
22+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
23+
</field>
2024
</group>
2125
</section>
2226
</system>

app/code/Magento/Swatches/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<frontend>
1212
<swatches_per_product>16</swatches_per_product>
1313
<show_swatches_in_product_list>1</show_swatches_in_product_list>
14+
<show_swatch_tooltip>1</show_swatch_tooltip>
1415
</frontend>
1516
</catalog>
1617
<general>

app/code/Magento/Swatches/view/frontend/templates/product/listing/renderer.phtml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ $productId = $block->getProduct()->getId();
2222
"jsonConfig": <?= /* @noEscape */ $block->getJsonConfig() ?>,
2323
"jsonSwatchConfig": <?= /* @noEscape */ $block->getJsonSwatchConfig() ?>,
2424
"mediaCallback": "<?= $block->escapeJs($block->escapeUrl($block->getMediaCallback())) ?>",
25-
"jsonSwatchImageSizeConfig": <?= /* @noEscape */ $block->getJsonSwatchSizeConfig() ?>
25+
"jsonSwatchImageSizeConfig": <?= /* @noEscape */ $block->getJsonSwatchSizeConfig() ?>,
26+
"showTooltip": <?= $block->escapeJs($block->getShowSwatchTooltip()) ?>
2627
}
2728
}
2829
}
@@ -39,4 +40,4 @@ $productId = $block->getProduct()->getId();
3940
}
4041
}
4142
}
42-
</script>
43+
</script>

app/code/Magento/Swatches/view/frontend/templates/product/view/renderer.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"jsonSwatchConfig": <?= /* @noEscape */ $swatchOptions = $block->getJsonSwatchConfig() ?>,
1616
"mediaCallback": "<?= $block->escapeJs($block->escapeUrl($block->getMediaCallback())) ?>",
1717
"gallerySwitchStrategy": "<?= $block->escapeJs($block->getVar('gallery_switch_strategy', 'Magento_ConfigurableProduct')) ?: 'replace'; ?>",
18-
"jsonSwatchImageSizeConfig": <?= /* @noEscape */ $block->getJsonSwatchSizeConfig() ?>
18+
"jsonSwatchImageSizeConfig": <?= /* @noEscape */ $block->getJsonSwatchSizeConfig() ?>,
19+
"showTooltip": <?= $block->escapeJs($block->getShowSwatchTooltip()) ?>
1920
}
2021
},
2122
"*" : {

app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ define([
385385
var $widget = this,
386386
container = this.element,
387387
classes = this.options.classes,
388-
chooseText = this.options.jsonConfig.chooseText;
388+
chooseText = this.options.jsonConfig.chooseText,
389+
showTooltip = this.options.showTooltip;
389390

390391
$widget.optionsMap = {};
391392

@@ -452,10 +453,12 @@ define([
452453
});
453454
});
454455

455-
// Connect Tooltip
456-
container
457-
.find('[option-type="1"], [option-type="2"], [option-type="0"], [option-type="3"]')
458-
.SwatchRendererTooltip();
456+
if (showTooltip === 1) {
457+
// Connect Tooltip
458+
container
459+
.find('[option-type="1"], [option-type="2"], [option-type="0"], [option-type="3"]')
460+
.SwatchRendererTooltip();
461+
}
459462

460463
// Hide all elements below more button
461464
$('.' + classes.moreButton).nextAll().hide();

0 commit comments

Comments
 (0)