@@ -278,6 +278,7 @@ protected function getCacheKey($data)
278
278
*/
279
279
protected function initializeProductData (array $ productData , $ createNew )
280
280
{
281
+ unset($ productData ['media_gallery ' ]);
281
282
if ($ createNew ) {
282
283
$ product = $ this ->productFactory ->create ();
283
284
if ($ this ->storeManager ->hasSingleStore ()) {
@@ -420,8 +421,15 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
420
421
}
421
422
422
423
/**
423
- * @param ProductInterface $product
424
- * @param array $mediaGalleryEntries
424
+ * Process Media gallery data before save product.
425
+ *
426
+ * Compare Media Gallery Entries Data with existing Media Gallery
427
+ * * If Media entry has not value_id set it as new
428
+ * * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
429
+ * * Merge Existing and new media gallery
430
+ *
431
+ * @param ProductInterface $product contains only existing media gallery items
432
+ * @param array $mediaGalleryEntries array which contains all media gallery items
425
433
* @return $this
426
434
* @throws InputException
427
435
* @throws StateException
@@ -431,11 +439,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
431
439
{
432
440
$ existingMediaGallery = $ product ->getMediaGallery ('images ' );
433
441
$ newEntries = [];
442
+ $ entriesById = [];
434
443
if (!empty ($ existingMediaGallery )) {
435
- $ entriesById = [];
436
444
foreach ($ mediaGalleryEntries as $ entry ) {
437
- if (isset ($ entry ['id ' ])) {
438
- $ entry ['value_id ' ] = $ entry ['id ' ];
445
+ if (isset ($ entry ['value_id ' ])) {
439
446
$ entriesById [$ entry ['value_id ' ]] = $ entry ;
440
447
} else {
441
448
$ newEntries [] = $ entry ;
@@ -444,6 +451,9 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
444
451
foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
445
452
if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
446
453
$ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
454
+ if ($ updatedEntry ['file ' ] === null ) {
455
+ unset($ updatedEntry ['file ' ]);
456
+ }
447
457
$ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
448
458
} else {
449
459
//set the removed flag
@@ -471,11 +481,18 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
471
481
}
472
482
/** @var ImageContentInterface $contentDataObject */
473
483
$ contentDataObject = $ this ->contentFactory ->create ()
474
- ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
475
- ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
476
- ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
484
+ ->setName ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::NAME ])
485
+ ->setBase64EncodedData ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::BASE64_ENCODED_DATA ])
486
+ ->setType ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::TYPE ]);
477
487
$ newEntry ['content ' ] = $ contentDataObject ;
478
488
$ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
489
+
490
+ $ finalGallery = $ product ->getData ('media_gallery ' );
491
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
492
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
493
+ $ entriesById [$ newEntryId ] = $ newEntry ;
494
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
495
+ $ product ->setData ('media_gallery ' , $ finalGallery );
479
496
}
480
497
return $ this ;
481
498
}
@@ -503,8 +520,6 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
503
520
$ productDataArray = $ this ->extensibleDataObjectConverter
504
521
->toNestedArray ($ product , [], 'Magento\Catalog\Api\Data\ProductInterface ' );
505
522
$ productDataArray = array_replace ($ productDataArray , $ product ->getData ());
506
- unset($ productDataArray ['media_gallery ' ]);
507
-
508
523
$ ignoreLinksFlag = $ product ->getData ('ignore_links_flag ' );
509
524
$ productLinks = null ;
510
525
if (!$ ignoreLinksFlag && $ ignoreLinksFlag !== null ) {
@@ -514,8 +529,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
514
529
$ product = $ this ->initializeProductData ($ productDataArray , empty ($ existingProduct ));
515
530
516
531
$ this ->processLinks ($ product , $ productLinks );
517
- if (isset ($ productDataArray ['media_gallery_entries ' ])) {
518
- $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery_entries ' ]);
532
+ if (isset ($ productDataArray ['media_gallery ' ])) {
533
+ $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery ' ][ ' images ' ]);
519
534
}
520
535
521
536
if (!$ product ->getOptionsReadonly ()) {
0 commit comments