Skip to content

Commit 930f1f8

Browse files
Shkolyarenko, Serhiy(sshkolyarenko)Shkolyarenko, Serhiy(sshkolyarenko)
authored andcommitted
Merge pull request #155 from magento-goinc/goinc_merchant_beta
MAGETWO-45172: No proper indication about why an image is not deleted when it is in use in one of the storeviews
2 parents 55276fd + cb330e6 commit 930f1f8

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product
2626
*/
2727
protected $productTypeManager;
2828

29+
/**
30+
* @var \Magento\Catalog\Api\ProductRepositoryInterface
31+
*/
32+
protected $productRepository;
33+
2934
/**
3035
* @param Action\Context $context
3136
* @param Builder $productBuilder
3237
* @param Initialization\Helper $initializationHelper
3338
* @param \Magento\Catalog\Model\Product\Copier $productCopier
3439
* @param \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
40+
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
3541
*/
3642
public function __construct(
3743
\Magento\Backend\App\Action\Context $context,
3844
Product\Builder $productBuilder,
3945
Initialization\Helper $initializationHelper,
4046
\Magento\Catalog\Model\Product\Copier $productCopier,
41-
\Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
47+
\Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager,
48+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
4249
) {
4350
$this->initializationHelper = $initializationHelper;
4451
$this->productCopier = $productCopier;
4552
$this->productTypeManager = $productTypeManager;
53+
$this->productRepository = $productRepository;
4654
parent::__construct($context, $productBuilder);
4755
}
4856

@@ -72,6 +80,7 @@ public function execute()
7280

7381
$originalSku = $product->getSku();
7482
$product->save();
83+
$this->handleImageRemoveError($data, $product->getId());
7584
$productId = $product->getId();
7685

7786
/**
@@ -140,4 +149,33 @@ public function execute()
140149
}
141150
return $resultRedirect;
142151
}
152+
153+
/**
154+
* Notify customer when image was not deleted in specific case.
155+
* TODO: temporary workaround must be eliminated in MAGETWO-45306
156+
*
157+
* @param array $postData
158+
* @param int $productId
159+
* @return void
160+
*/
161+
private function handleImageRemoveError($postData, $productId)
162+
{
163+
if (isset($postData['product']['media_gallery']['images'])) {
164+
$removedImagesAmount = 0;
165+
foreach ($postData['product']['media_gallery']['images'] as $image) {
166+
if (!empty($image['removed'])) {
167+
$removedImagesAmount++;
168+
}
169+
}
170+
if ($removedImagesAmount) {
171+
$expectedImagesAmount = count($postData['product']['media_gallery']['images']) - $removedImagesAmount;
172+
$product = $this->productRepository->getById($productId);
173+
if ($expectedImagesAmount != count($product->getMediaGallery('images'))) {
174+
$this->messageManager->addNotice(
175+
__('The image cannot be removed as it has been assigned to the other image role')
176+
);
177+
}
178+
}
179+
}
180+
}
143181
}

app/code/Magento/Catalog/Model/Product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,11 +1512,11 @@ public function getMediaGalleryImages()
15121512
if (!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
15131513
$images = $this->_collectionFactory->create();
15141514
foreach ($this->getMediaGallery('images') as $image) {
1515-
if (isset($image['disabled']) && $image['disabled']) {
1515+
if ((isset($image['disabled']) && $image['disabled']) || empty($image['value_id'])) {
15161516
continue;
15171517
}
15181518
$image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
1519-
$image['id'] = isset($image['value_id']) ? $image['value_id'] : null;
1519+
$image['id'] = $image['value_id'];
15201520
$image['path'] = $directory->getAbsolutePath($this->getMediaConfig()->getMediaPath($image['file']));
15211521
$images->addItem(new \Magento\Framework\Object($image));
15221522
}

0 commit comments

Comments
 (0)