Skip to content

Commit f380abf

Browse files
MAGETWO-59789: Image Swatch size change not working
- Add swatch image config size
1 parent 03ab0fd commit f380abf

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,22 @@ public function getIdentities()
473473
return [];
474474
}
475475
}
476+
477+
/**
478+
* Get Swatch image size config data.
479+
*
480+
* @return string
481+
*/
482+
public function getJsonSwatchSizeConfig()
483+
{
484+
$imageConfig = $this->swatchMediaHelper->getImageConfig();
485+
$sizeConfig = [];
486+
487+
$sizeConfig[Swatch::SWATCH_IMAGE_NAME]['width'] = $imageConfig[Swatch::SWATCH_IMAGE_NAME]['width'];
488+
$sizeConfig[Swatch::SWATCH_IMAGE_NAME]['height'] = $imageConfig[Swatch::SWATCH_IMAGE_NAME]['height'];
489+
$sizeConfig[Swatch::SWATCH_THUMBNAIL_NAME]['height'] = $imageConfig[Swatch::SWATCH_THUMBNAIL_NAME]['height'];
490+
$sizeConfig[Swatch::SWATCH_THUMBNAIL_NAME]['width'] = $imageConfig[Swatch::SWATCH_THUMBNAIL_NAME]['width'];
491+
492+
return $this->jsonEncoder->encode($sizeConfig);
493+
}
476494
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
echo $swatchOptions = $block->getJsonSwatchConfig(); ?>,
1919
"mediaCallback": "<?= /* @escapeNotVerified */ $block->getMediaCallback() ?>",
2020
"gallerySwitchStrategy": "<?php /* @escapeNotVerified */ echo $block->getVar('gallery_switch_strategy',
21-
'Magento_ConfigurableProduct') ?: 'replace'; ?>"
21+
'Magento_ConfigurableProduct') ?: 'replace'; ?>",
22+
"jsonSwatchImageSizeConfig": <?php /* @escapeNotVerified */
23+
echo $block->getJsonSwatchSizeConfig() ?>
2224
}
2325
},
2426
"*" : {

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ define([
6767
* - option-label (string)
6868
* - option-tooltip-thumb
6969
* - option-tooltip-value
70+
* - thumb-width
71+
* - thumb-height
7072
*/
7173
$.widget('mage.SwatchRendererTooltip', {
7274
options: {
@@ -86,6 +88,8 @@ define([
8688
label = $this.attr('option-label'),
8789
thumb = $this.attr('option-tooltip-thumb'),
8890
value = $this.attr('option-tooltip-value'),
91+
width = $this.attr('thumb-width'),
92+
height = $this.attr('thumb-height'),
8993
$image,
9094
$title,
9195
$corner;
@@ -115,7 +119,9 @@ define([
115119
// Image
116120
$image.css({
117121
'background': 'url("' + thumb + '") no-repeat center', //Background case
118-
'background-size': 'initial'
122+
'background-size': 'initial',
123+
'width': width + 'px',
124+
'height': height +'px'
119125
});
120126
$image.show();
121127
} else if (type === 1) {
@@ -476,6 +482,7 @@ define([
476482
_RenderSwatchOptions: function (config, controlId) {
477483
var optionConfig = this.options.jsonSwatchConfig[config.id],
478484
optionClass = this.options.classes.optionClass,
485+
sizeConfig = this.options.jsonSwatchImageSizeConfig,
479486
moreLimit = parseInt(this.options.numberToShow, 10),
480487
moreClass = this.options.classes.moreButton,
481488
moreText = this.options.moreButtonText,
@@ -492,6 +499,8 @@ define([
492499
value,
493500
thumb,
494501
label,
502+
width,
503+
height,
495504
attr;
496505

497506
if (!optionConfig.hasOwnProperty(this.id)) {
@@ -507,6 +516,8 @@ define([
507516
type = parseInt(optionConfig[id].type, 10);
508517
value = optionConfig[id].hasOwnProperty('value') ? optionConfig[id].value : '';
509518
thumb = optionConfig[id].hasOwnProperty('thumb') ? optionConfig[id].thumb : '';
519+
width = sizeConfig.swatch_thumb.width;
520+
height = sizeConfig.swatch_thumb.height;
510521
label = this.label ? this.label : '';
511522
attr =
512523
' id="' + controlId + '-item-' + id + '"' +
@@ -519,7 +530,9 @@ define([
519530
' aria-label="' + label + '"' +
520531
' option-tooltip-thumb="' + thumb + '"' +
521532
' option-tooltip-value="' + value + '"' +
522-
' role="option"';
533+
' role="option"' +
534+
' thumb-width="' + width + '"' +
535+
' thumb-height="' + height + '"';
523536

524537
if (!this.hasOwnProperty('products') || this.products.length <= 0) {
525538
attr += ' option-empty="true"';
@@ -538,7 +551,8 @@ define([
538551
} else if (type === 2) {
539552
// Image
540553
html += '<div class="' + optionClass + ' image" ' + attr +
541-
' style="background: url(' + value + ') no-repeat center; background-size: initial;">' + '' +
554+
' style="background: url(' + value + ') no-repeat center; background-size: initial;width:'
555+
+ sizeConfig.swatch_image.width + 'px; height:'+ sizeConfig.swatch_image.height + 'px">' + '' +
542556
'</div>';
543557
} else if (type === 3) {
544558
// Clear

0 commit comments

Comments
 (0)