Skip to content

Commit bf0a620

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-62587' into 2.0.15-develop-pr14
2 parents 3f2c094 + eb6095c commit bf0a620

File tree

3 files changed

+194
-88
lines changed

3 files changed

+194
-88
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ protected function getCacheKey($data)
283283
*/
284284
protected function initializeProductData(array $productData, $createNew)
285285
{
286+
unset($productData['media_gallery']);
286287
if ($createNew) {
287288
$product = $this->productFactory->create();
288289
if ($this->storeManager->hasSingleStore()) {
@@ -503,11 +504,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
503504
{
504505
$existingMediaGallery = $product->getMediaGallery('images');
505506
$newEntries = [];
507+
$entriesById = [];
506508
if (!empty($existingMediaGallery)) {
507-
$entriesById = [];
508509
foreach ($mediaGalleryEntries as $entry) {
509-
if (isset($entry['id'])) {
510-
$entry['value_id'] = $entry['id'];
510+
if (isset($entry['value_id'])) {
511511
$entriesById[$entry['value_id']] = $entry;
512512
} else {
513513
$newEntries[] = $entry;
@@ -516,6 +516,9 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
516516
foreach ($existingMediaGallery as $key => &$existingEntry) {
517517
if (isset($entriesById[$existingEntry['value_id']])) {
518518
$updatedEntry = $entriesById[$existingEntry['value_id']];
519+
if ($updatedEntry['file'] === null) {
520+
unset($updatedEntry['file']);
521+
}
519522
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
520523
} else {
521524
//set the removed flag
@@ -545,11 +548,18 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
545548
}
546549
/** @var ImageContentInterface $contentDataObject */
547550
$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]);
551554
$newEntry['content'] = $contentDataObject;
552555
$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);
553563
}
554564
return $this;
555565
}
@@ -570,8 +580,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
570580
$productId = $this->resourceModel->getIdBySku($product->getSku());
571581
$ignoreLinksFlag = $product->getData('ignore_links_flag');
572582
$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());
575585
$productLinks = null;
576586
if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
577587
$productLinks = $product->getProductLinks();
@@ -588,8 +598,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
588598
}
589599

590600
$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']);
593603
}
594604

595605
$validationResult = $this->resourceModel->validate($product);

0 commit comments

Comments
 (0)