Skip to content

Commit 279522a

Browse files
committed
MAGETWO-83204: "Hide from Product Page" option does not work for child of configurable product
1 parent 4be3810 commit 279522a

File tree

1 file changed

+40
-35
lines changed
  • app/code/Magento/Swatches/Helper

1 file changed

+40
-35
lines changed

app/code/Magento/Swatches/Helper/Data.php

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@ private function populateAdditionalDataEavAttribute(Attribute $attribute)
189189
return $this;
190190
}
191191

192+
/**
193+
* @param ModelProduct $product
194+
* @param string $attributeCode
195+
* @return bool
196+
*/
197+
private function isMediaAvailable(ModelProduct $product, $attributeCode)
198+
{
199+
$isAvailable = false;
200+
201+
$mediaGallery = $product->getMediaGalleryEntries();
202+
foreach ($mediaGallery as $mediaEntry) {
203+
if (in_array($attributeCode, $mediaEntry->getTypes(), true)) {
204+
$isAvailable = !$mediaEntry->isDisabled();
205+
break;
206+
}
207+
}
208+
209+
return $isAvailable;
210+
}
211+
192212
/**
193213
* @param string $attributeCode swatch_image|image
194214
* @param ModelProduct $configurableProduct
@@ -201,8 +221,9 @@ private function loadFirstVariation($attributeCode, ModelProduct $configurablePr
201221
$usedProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
202222

203223
foreach ($usedProducts as $simpleProduct) {
204-
if (!in_array($simpleProduct->getData($attributeCode), [null, self::EMPTY_IMAGE_VALUE], true)
205-
&& !array_diff_assoc($requiredAttributes, $simpleProduct->getData())
224+
if (
225+
!array_diff_assoc($requiredAttributes, $simpleProduct->getData())
226+
&& $this->isMediaAvailable($simpleProduct, $attributeCode)
206227
) {
207228
return $simpleProduct;
208229
}
@@ -313,50 +334,34 @@ private function addFilterByParent(ProductCollection $productCollection, $parent
313334
*/
314335
public function getProductMediaGallery(ModelProduct $product)
315336
{
316-
if (!in_array($product->getData('image'), [null, self::EMPTY_IMAGE_VALUE], true)) {
317-
$baseImage = $product->getData('image');
318-
} else {
319-
$productMediaAttributes = array_filter($product->getMediaAttributeValues(), function ($value) {
320-
return $value !== self::EMPTY_IMAGE_VALUE && $value !== null;
321-
});
322-
foreach ($productMediaAttributes as $attributeCode => $value) {
323-
if ($attributeCode !== 'swatch_image') {
324-
$baseImage = (string)$value;
325-
break;
326-
}
337+
$baseImage = null;
338+
$gallery = [];
339+
340+
$mediaGallery = $product->getMediaGalleryEntries();
341+
foreach ($mediaGallery as $mediaEntry) {
342+
if ($mediaEntry->isDisabled()) {
343+
continue;
327344
}
345+
346+
if (in_array('image', $mediaEntry->getTypes(), true)) {
347+
$baseImage = $mediaEntry->getFile();
348+
} elseif (!$baseImage) {
349+
$baseImage = $mediaEntry->getFile();
350+
}
351+
352+
$gallery[$mediaEntry->getId()] = $this->getAllSizeImages($product, $mediaEntry->getFile());
328353
}
329354

330-
if (empty($baseImage)) {
355+
if (!$baseImage) {
331356
return [];
332357
}
333358

334359
$resultGallery = $this->getAllSizeImages($product, $baseImage);
335-
$resultGallery['gallery'] = $this->getGalleryImages($product);
360+
$resultGallery['gallery'] = $gallery;
336361

337362
return $resultGallery;
338363
}
339364

340-
/**
341-
* @param ModelProduct $product
342-
* @return array
343-
*/
344-
private function getGalleryImages(ModelProduct $product)
345-
{
346-
//TODO: remove after fix MAGETWO-48040
347-
$product = $this->productRepository->getById($product->getId());
348-
349-
$result = [];
350-
$mediaGallery = $product->getMediaGalleryImages();
351-
foreach ($mediaGallery as $media) {
352-
$result[$media->getData('value_id')] = $this->getAllSizeImages(
353-
$product,
354-
$media->getData('file')
355-
);
356-
}
357-
return $result;
358-
}
359-
360365
/**
361366
* @param ModelProduct $product
362367
* @param string $imageFile

0 commit comments

Comments
 (0)