@@ -283,6 +283,7 @@ protected function getCacheKey($data)
283
283
*/
284
284
protected function initializeProductData (array $ productData , $ createNew )
285
285
{
286
+ unset($ productData ['media_gallery ' ]);
286
287
if ($ createNew ) {
287
288
$ product = $ this ->productFactory ->create ();
288
289
if ($ this ->storeManager ->hasSingleStore ()) {
@@ -503,11 +504,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
503
504
{
504
505
$ existingMediaGallery = $ product ->getMediaGallery ('images ' );
505
506
$ newEntries = [];
507
+ $ entriesById = [];
506
508
if (!empty ($ existingMediaGallery )) {
507
- $ entriesById = [];
508
509
foreach ($ mediaGalleryEntries as $ entry ) {
509
- if (isset ($ entry ['id ' ])) {
510
- $ entry ['value_id ' ] = $ entry ['id ' ];
510
+ if (isset ($ entry ['value_id ' ])) {
511
511
$ entriesById [$ entry ['value_id ' ]] = $ entry ;
512
512
} else {
513
513
$ newEntries [] = $ entry ;
@@ -516,6 +516,9 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
516
516
foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
517
517
if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
518
518
$ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
519
+ if ($ updatedEntry ['file ' ] === null ) {
520
+ unset($ updatedEntry ['file ' ]);
521
+ }
519
522
$ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
520
523
} else {
521
524
//set the removed flag
@@ -545,11 +548,18 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
545
548
}
546
549
/** @var ImageContentInterface $contentDataObject */
547
550
$ contentDataObject = $ this ->contentFactory ->create ()
548
- ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
549
- ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
550
- ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
551
+ ->setName ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::NAME ])
552
+ ->setBase64EncodedData ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::BASE64_ENCODED_DATA ])
553
+ ->setType ($ newEntry ['content ' ][' data ' ][ ImageContentInterface::TYPE ]);
551
554
$ newEntry ['content ' ] = $ contentDataObject ;
552
555
$ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
556
+
557
+ $ finalGallery = $ product ->getData ('media_gallery ' );
558
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
559
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
560
+ $ entriesById [$ newEntryId ] = $ newEntry ;
561
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
562
+ $ product ->setData ('media_gallery ' , $ finalGallery );
553
563
}
554
564
return $ this ;
555
565
}
@@ -570,8 +580,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
570
580
$ productId = $ this ->resourceModel ->getIdBySku ($ product ->getSku ());
571
581
$ ignoreLinksFlag = $ product ->getData ('ignore_links_flag ' );
572
582
$ productDataArray = $ this ->extensibleDataObjectConverter
573
- ->toNestedArray ($ product , [], ' Magento\Catalog\Api\Data\ProductInterface ' );
574
-
583
+ ->toNestedArray ($ product , [], \ Magento \Catalog \Api \Data \ProductInterface::class );
584
+ $ productDataArray = array_replace ( $ productDataArray , $ product -> getData ());
575
585
$ productLinks = null ;
576
586
if (!$ ignoreLinksFlag && $ ignoreLinksFlag !== null ) {
577
587
$ productLinks = $ product ->getProductLinks ();
@@ -588,8 +598,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
588
598
}
589
599
590
600
$ this ->processLinks ($ product , $ productLinks );
591
- if (isset ($ productDataArray ['media_gallery_entries ' ])) {
592
- $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery_entries ' ]);
601
+ if (isset ($ productDataArray ['media_gallery ' ])) {
602
+ $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery ' ][ ' images ' ]);
593
603
}
594
604
595
605
$ validationResult = $ this ->resourceModel ->validate ($ product );
0 commit comments