Skip to content

Commit 0e2a8d2

Browse files
author
Michail Slabko
committed
MAGETWO-44868: Swatches thumbnails aren't shown on frontend
1 parent 777f782 commit 0e2a8d2

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

app/code/Magento/Catalog/Helper/Image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public function init($product, $imageId, $attributes = [])
176176
$this->_reset();
177177

178178
$this->attributes = array_merge(
179-
$attributes,
180-
$this->getConfigView()->getMediaAttributes('Magento_Catalog', self::MEDIA_TYPE_CONFIG_NODE, $imageId)
179+
$this->getConfigView()->getMediaAttributes('Magento_Catalog', self::MEDIA_TYPE_CONFIG_NODE, $imageId),
180+
$attributes
181181
);
182182

183183
$this->setProduct($product);

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ protected function extractNecessarySwatchData(array $swatchDataArray)
253253

254254
if ($result['type'] == Swatch::SWATCH_TYPE_VISUAL_IMAGE && !empty($swatchDataArray['value'])) {
255255
$result['value'] = $this->swatchMediaHelper->getSwatchAttributeImage(
256-
'swatch_image',
256+
Swatch::SWATCH_IMAGE_NAME,
257257
$swatchDataArray['value']
258258
);
259259
$result['thumb'] = $this->swatchMediaHelper->getSwatchAttributeImage(
260-
'swatch_thumb',
260+
Swatch::SWATCH_THUMBNAIL_NAME,
261261
$swatchDataArray['value']
262262
);
263263
} else {
@@ -284,8 +284,8 @@ protected function getVariationMedia($attributeCode, $optionId)
284284
$variationMediaArray = [];
285285
if ($variationProduct) {
286286
$variationMediaArray = [
287-
'value' => $this->getSwatchProductImage($variationProduct, 'swatch_image'),
288-
'thumb' => $this->getSwatchProductImage($variationProduct, 'swatch_thumb'),
287+
'value' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_IMAGE_NAME),
288+
'thumb' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_THUMBNAIL_NAME),
289289
];
290290
}
291291

@@ -299,22 +299,28 @@ protected function getVariationMedia($attributeCode, $optionId)
299299
*/
300300
protected function getSwatchProductImage(Product $childProduct, $imageType)
301301
{
302-
if (
303-
$childProduct->getData('swatch_image') !== null
304-
&& $childProduct->getData('swatch_image') != self::EMPTY_IMAGE_VALUE
305-
) {
306-
$swatchImageId = $imageType == 'swatch_image' ? 'swatch_image' : 'swatch_thumb';
307-
} elseif (
308-
$childProduct->getData('image') !== null
309-
&& $childProduct->getData('image') != self::EMPTY_IMAGE_VALUE
310-
) {
311-
$swatchImageId = $imageType == 'swatch_image' ? 'swatch_image_base' : 'swatch_thumb_base';
302+
if ($this->isProductHasImage($childProduct, Swatch::SWATCH_IMAGE_NAME)) {
303+
$swatchImageId = $imageType;
304+
$imageAttributes = ['type' => Swatch::SWATCH_IMAGE_NAME];
305+
} elseif ($this->isProductHasImage($childProduct, 'image')) {
306+
$swatchImageId = $imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base' : 'swatch_thumb_base';
307+
$imageAttributes = ['type' => 'image'];
312308
}
313309
if (isset($swatchImageId)) {
314-
return $this->_imageHelper->init($childProduct, $swatchImageId)->getUrl();
310+
return $this->_imageHelper->init($childProduct, $swatchImageId, $imageAttributes)->getUrl();
315311
}
316312
}
317313

314+
/**
315+
* @param Product $product
316+
* @param string $imageType
317+
* @return bool
318+
*/
319+
protected function isProductHasImage(Product $product, $imageType)
320+
{
321+
return $product->getData($imageType) !== null && $product->getData($imageType) != self::EMPTY_IMAGE_VALUE;
322+
}
323+
318324
/**
319325
* @param array $attributeData
320326
* @return array

app/code/Magento/Swatches/Model/Plugin/Product.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
*/
1111
class Product
1212
{
13-
/**
14-
* Name of swatch image role
15-
*/
16-
const ROLE_SWATCH_IMAGE_NAME = 'swatch_image';
17-
1813
/**
1914
* Unset swatch image role if product is not simple
2015
*
@@ -28,7 +23,7 @@ public function afterGetMediaAttributes(\Magento\Catalog\Model\Product $product,
2823
&& $product->getTypeId() !== \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL
2924
) {
3025
if (is_array($imageRoles)) {
31-
unset($imageRoles[self::ROLE_SWATCH_IMAGE_NAME]);
26+
unset($imageRoles[\Magento\Swatches\Model\Swatch::SWATCH_IMAGE_NAME]);
3227
}
3328
}
3429

app/code/Magento/Swatches/Model/Swatch.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class Swatch extends \Magento\Framework\Model\AbstractModel
4040
/** Constant for identifying empty swatch type */
4141
const SWATCH_TYPE_EMPTY = 3;
4242

43+
/**
44+
* Name of swatch image
45+
*/
46+
const SWATCH_IMAGE_NAME = 'swatch_image';
47+
48+
/**
49+
* Name of swatch thumbnail
50+
*/
51+
const SWATCH_THUMBNAIL_NAME = 'swatch_thumb';
52+
4353
/**
4454
* Initialize resource model
4555
*

app/code/Magento/Swatches/Test/Unit/Block/Product/Renderer/ConfigurableTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public function testGetJsonSwatchConfigNotVisualImageType()
176176

177177
$this->imageHelper->expects($this->exactly(2))->method('init')
178178
->willReturnMap([
179-
[$this->product, 'swatch_image', [], $this->imageHelper],
180-
[$this->product, 'swatch_thumb', [], $this->imageHelper],
179+
[$this->product, 'swatch_image', ['type' => 'swatch_image'], $this->imageHelper],
180+
[$this->product, 'swatch_thumb', ['type' => 'swatch_image'], $this->imageHelper],
181181
]);
182182

183183
$this->jsonEncoder->expects($this->once())->method('encode');
@@ -222,8 +222,8 @@ public function testGetJsonSwatchConfigVisualImageType()
222222

223223
$this->imageHelper->expects($this->exactly(2))->method('init')
224224
->willReturnMap([
225-
[$this->product, 'swatch_image_base', [], $this->imageHelper],
226-
[$this->product, 'swatch_thumb_base', [], $this->imageHelper],
225+
[$this->product, 'swatch_image_base', ['type' => 'image'], $this->imageHelper],
226+
[$this->product, 'swatch_thumb_base', ['type' => 'image'], $this->imageHelper],
227227
]);
228228

229229
$this->jsonEncoder->expects($this->once())->method('encode');

0 commit comments

Comments
 (0)