@@ -289,6 +289,7 @@ protected function getCacheKey($data)
289
289
*/
290
290
protected function initializeProductData (array $ productData , $ createNew )
291
291
{
292
+ unset($ productData ['media_gallery ' ]);
292
293
if ($ createNew ) {
293
294
$ product = $ this ->productFactory ->create ();
294
295
if ($ this ->storeManager ->hasSingleStore ()) {
@@ -437,8 +438,15 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
437
438
}
438
439
439
440
/**
440
- * @param ProductInterface $product
441
- * @param array $mediaGalleryEntries
441
+ * Process Media gallery data before save product.
442
+ *
443
+ * Compare Media Gallery Entries Data with existing Media Gallery
444
+ * * If Media entry has not value_id set it as new
445
+ * * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
446
+ * * Merge Existing and new media gallery
447
+ *
448
+ * @param ProductInterface $product contains only existing media gallery items
449
+ * @param array $mediaGalleryEntries array which contains all media gallery items
442
450
* @return $this
443
451
* @throws InputException
444
452
* @throws StateException
@@ -448,11 +456,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
448
456
{
449
457
$ existingMediaGallery = $ product ->getMediaGallery ('images ' );
450
458
$ newEntries = [];
459
+ $ entriesById = [];
451
460
if (!empty ($ existingMediaGallery )) {
452
- $ entriesById = [];
453
461
foreach ($ mediaGalleryEntries as $ entry ) {
454
- if (isset ($ entry ['id ' ])) {
455
- $ entry ['value_id ' ] = $ entry ['id ' ];
462
+ if (isset ($ entry ['value_id ' ])) {
456
463
$ entriesById [$ entry ['value_id ' ]] = $ entry ;
457
464
} else {
458
465
$ newEntries [] = $ entry ;
@@ -461,6 +468,9 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
461
468
foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
462
469
if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
463
470
$ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
471
+ if ($ updatedEntry ['file ' ] === null ) {
472
+ unset($ updatedEntry ['file ' ]);
473
+ }
464
474
$ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
465
475
} else {
466
476
//set the removed flag
@@ -488,11 +498,18 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
488
498
}
489
499
/** @var ImageContentInterface $contentDataObject */
490
500
$ contentDataObject = $ this ->contentFactory ->create ()
491
- ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
492
- ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
493
- ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
501
+ ->setName ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::NAME ])
502
+ ->setBase64EncodedData ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::BASE64_ENCODED_DATA ])
503
+ ->setType ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::TYPE ]);
494
504
$ newEntry ['content ' ] = $ contentDataObject ;
495
505
$ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
506
+
507
+ $ finalGallery = $ product ->getData ('media_gallery ' );
508
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
509
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
510
+ $ entriesById [$ newEntryId ] = $ newEntry ;
511
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
512
+ $ product ->setData ('media_gallery ' , $ finalGallery );
496
513
}
497
514
return $ this ;
498
515
}
@@ -520,8 +537,6 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
520
537
$ productDataArray = $ this ->extensibleDataObjectConverter
521
538
->toNestedArray ($ product , [], \Magento \Catalog \Api \Data \ProductInterface::class);
522
539
$ productDataArray = array_replace ($ productDataArray , $ product ->getData ());
523
- unset($ productDataArray ['media_gallery ' ]);
524
-
525
540
$ ignoreLinksFlag = $ product ->getData ('ignore_links_flag ' );
526
541
$ productLinks = null ;
527
542
if (!$ ignoreLinksFlag && $ ignoreLinksFlag !== null ) {
@@ -531,8 +546,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
531
546
$ product = $ this ->initializeProductData ($ productDataArray , empty ($ existingProduct ));
532
547
533
548
$ this ->processLinks ($ product , $ productLinks );
534
- if (isset ($ productDataArray ['media_gallery_entries ' ])) {
535
- $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery_entries ' ]);
549
+ if (isset ($ productDataArray ['media_gallery ' ])) {
550
+ $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery ' ][ ' images ' ]);
536
551
}
537
552
538
553
if (!$ product ->getOptionsReadonly ()) {
0 commit comments