@@ -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}
0 commit comments