Skip to content

Commit 7959af7

Browse files
committed
10797: catalogProductTierPriceManagementV1 DELETE and POST operation wipes out media gallery selections when used on store code "all".
1 parent c45ec2a commit 7959af7

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ protected function processMediaGallery(ProductInterface $product, array $mediaGa
507507
foreach ($existingMediaGallery as $key => &$existingEntry) {
508508
if (isset($entriesById[$existingEntry['value_id']])) {
509509
$updatedEntry = $entriesById[$existingEntry['value_id']];
510-
if ($updatedEntry['file'] === null) {
510+
if (isset($updatedEntry['file']) && $updatedEntry['file'] === null) {
511511
unset($updatedEntry['file']);
512512
}
513513
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
@@ -546,6 +546,9 @@ protected function processMediaGallery(ProductInterface $product, array $mediaGa
546546

547547
$finalGallery = $product->getData('media_gallery');
548548
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
549+
if (isset($newEntry['extension_attributes'])) {
550+
$this->processExtensionAttributes($newEntry, $newEntry['extension_attributes']);
551+
}
549552
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
550553
$entriesById[$newEntryId] = $newEntry;
551554
$finalGallery['images'][$newEntryId] = $newEntry;
@@ -788,4 +791,23 @@ private function getCollectionProcessor()
788791
}
789792
return $this->collectionProcessor;
790793
}
794+
795+
/**
796+
* Convert extension attribute for product media gallery.
797+
*
798+
* @param array $newEntry
799+
* @param array $extensionAttributes
800+
* @return void
801+
*/
802+
private function processExtensionAttributes(array &$newEntry, array $extensionAttributes)
803+
{
804+
foreach ($extensionAttributes as $code => $value) {
805+
if (is_array($value)) {
806+
$this->processExtensionAttributes($newEntry, $value);
807+
} else {
808+
$newEntry[$code] = $value;
809+
}
810+
}
811+
unset($newEntry['extension_attributes']);
812+
}
791813
}

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace Magento\Catalog\Test\Unit\Model;
1111

12-
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1312
use Magento\Framework\Api\Data\ImageContentInterface;
1413
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1514
use Magento\Framework\DB\Adapter\ConnectionException;
@@ -1178,7 +1177,21 @@ public function testSaveExistingWithNewMediaGalleryEntries()
11781177

11791178
$this->setupProductMocksForSave();
11801179
//media gallery data
1181-
$this->productData['media_gallery'] = $newEntriesData;
1180+
$this->productData['media_gallery_entries'] = [
1181+
[
1182+
'id' => null,
1183+
'label' => "label_text",
1184+
'position' => 10,
1185+
'disabled' => false,
1186+
'types' => ['image', 'small_image'],
1187+
'content' => [
1188+
ImageContentInterface::NAME => 'filename',
1189+
ImageContentInterface::TYPE => 'image/jpeg',
1190+
ImageContentInterface::BASE64_ENCODED_DATA => 'encoded_content',
1191+
],
1192+
'media_type' => 'media_type',
1193+
]
1194+
];
11821195
$this->extensibleDataObjectConverterMock
11831196
->expects($this->once())
11841197
->method('toNestedArray')
@@ -1288,7 +1301,7 @@ public function testSaveExistingWithMediaGalleryEntries()
12881301
//update one entry, delete one entry
12891302
$newEntries = [
12901303
[
1291-
'value_id' => 5,
1304+
'id' => 5,
12921305
"label" => "new_label_text",
12931306
'file' => 'filename1',
12941307
'position' => 10,
@@ -1316,7 +1329,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13161329
$expectedResult = [
13171330
[
13181331
'value_id' => 5,
1319-
'value_id' => 5,
1332+
'id' => 5,
13201333
"label" => "new_label_text",
13211334
'file' => 'filename1',
13221335
'position' => 10,
@@ -1332,7 +1345,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13321345

13331346
$this->setupProductMocksForSave();
13341347
//media gallery data
1335-
$this->productData['media_gallery']['images'] = $newEntries;
1348+
$this->productData['media_gallery_entries'] = $newEntries;
13361349
$this->extensibleDataObjectConverterMock
13371350
->expects($this->once())
13381351
->method('toNestedArray')

0 commit comments

Comments
 (0)