Skip to content

Commit

Permalink
MAGETWO-44971: [GITHUB] Configurable product issues after saving #2226
Browse files Browse the repository at this point in the history
- fix for image gallery for configurable product
  • Loading branch information
Michail Slabko committed Nov 18, 2015
1 parent d4af71b commit 3d68d9c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ protected function getProduct()
* @param string $file
* @return $this
*/
protected function setImageFile($file)
public function setImageFile($file)
{
$this->_imageFile = $file;
return $this;
Expand Down
31 changes: 22 additions & 9 deletions app/code/Magento/ConfigurableProduct/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,41 @@ public function __construct(\Magento\Catalog\Helper\Image $imageHelper)
public function getOptions($currentProduct, $allowedProducts)
{
$options = [];
$baseImageUrl = (string)$this->imageHelper->init($currentProduct, 'image');

foreach ($allowedProducts as $product) {
$productId = $product->getId();
$image = (string)$this->imageHelper->init($product, 'image');

$images = $this->getGalleryImages($product);
$options['images'][$productId] = $images;
foreach ($this->getAllowAttributes($currentProduct) as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());

$options[$productAttributeId][$attributeValue][] = $productId;
$imageUrl = (!$product->getImage() || $product->getImage() === 'no_selection')
? $baseImageUrl
: (string)$image;
$options['images'][$productAttributeId][$attributeValue][$productId] = $imageUrl;
}
}

return $options;
}

/**
* Retrieve collection of gallery images
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return \Magento\Catalog\Model\Product\Image[]|null
*/
public function getGalleryImages(\Magento\Catalog\Api\Data\ProductInterface $product)
{
$images = [];
$imagesGallery = $product->getMediaGalleryImages();
if ($imagesGallery instanceof \Magento\Framework\Data\Collection) {
foreach ($imagesGallery as $image) {
/** @var $image \Magento\Catalog\Model\Product\Image */
$images[] = (string)$this->imageHelper->init($product, 'image')->setImageFile($image->getFile());
}
}

return $images;
}

/**
* Get allowed attributes
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ public function getUsedProducts($product, $requiredAttributeIds = null)
}

$usedProducts = [];
$collection = $this->getUsedProductCollection($product)->addAttributeToSelect('*')
$collection = $this->getUsedProductCollection($product)
->addAttributeToSelect('*')
->addAttributeToSelect('media_gallery')
->addFilterByRequiredOptions()
->setStoreId($product->getStoreId());

Expand All @@ -505,6 +507,8 @@ public function getUsedProducts($product, $requiredAttributeIds = null)
}

foreach ($collection as $item) {
/** @var \Magento\Catalog\Model\Product $item */
$item->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($item);
$usedProducts[] = $item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ define([
* @param element The element associated with a configurable option.
*/
_configureElement: function(element) {
this.inputSimpleProduct.val(null);
if (element.value) {
this.options.state[element.config.id] = element.value;
if (element.nextSetting) {
Expand All @@ -231,24 +232,11 @@ define([
_changeProductImage: function () {
var images = this.options.spConfig.images,
imagesArray = null,
galleryElement = $(this.options.mediaGallerySelector);
$.each(this.options.settings, function (k, v) {
var selectValue = parseInt(v.value, 10),
attributeId = v.id.replace(/[a-z]*/, '');
if (selectValue > 0 && attributeId) {
if (!imagesArray) {
imagesArray = images[attributeId][selectValue];
} else {
var intersectedArray = {};
$.each(imagesArray, function (productId) {
if (images[attributeId][selectValue][productId]) {
intersectedArray[productId] = images[attributeId][selectValue][productId];
}
});
imagesArray = intersectedArray;
}
}
});
galleryElement = $(this.options.mediaGallerySelector),
productId = parseInt(this.inputSimpleProduct.val());
if (productId > 0) {
imagesArray = images[productId];
}

var result = [];
$.each(imagesArray || {}, function (k, v) {
Expand Down

0 comments on commit 3d68d9c

Please sign in to comment.