Skip to content

Commit babf203

Browse files
committed
Merge remote-tracking branch 'origin/MC-29417' into 2.4-develop-pr2
2 parents d3e96c5 + bad9717 commit babf203

File tree

5 files changed

+111
-34
lines changed

5 files changed

+111
-34
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,13 @@ protected function _saveProducts()
15691569
continue;
15701570
}
15711571

1572+
$storeId = !empty($rowData[self::COL_STORE])
1573+
? $this->getStoreIdByCode($rowData[self::COL_STORE])
1574+
: Store::DEFAULT_STORE_ID;
1575+
$rowExistingImages = $existingImages[$storeId][$rowSku] ?? [];
1576+
$rowStoreMediaGalleryValues = $rowExistingImages;
1577+
$rowExistingImages += $existingImages[Store::DEFAULT_STORE_ID][$rowSku] ?? [];
1578+
15721579
if (self::SCOPE_STORE == $rowScope) {
15731580
// set necessary data from SCOPE_DEFAULT row
15741581
$rowData[self::COL_TYPE] = $this->skuProcessor->getNewSku($rowSku)['type_id'];
@@ -1672,19 +1679,16 @@ protected function _saveProducts()
16721679

16731680
// 5. Media gallery phase
16741681
list($rowImages, $rowLabels) = $this->getImagesFromRow($rowData);
1675-
$storeId = !empty($rowData[self::COL_STORE])
1676-
? $this->getStoreIdByCode($rowData[self::COL_STORE])
1677-
: Store::DEFAULT_STORE_ID;
16781682
$imageHiddenStates = $this->getImagesHiddenStates($rowData);
16791683
foreach (array_keys($imageHiddenStates) as $image) {
1680-
if (array_key_exists($rowSku, $existingImages)
1681-
&& array_key_exists($image, $existingImages[$rowSku])
1682-
) {
1683-
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
1684+
//Mark image as uploaded if it exists
1685+
if (array_key_exists($image, $rowExistingImages)) {
16841686
$uploadedImages[$image] = $image;
16851687
}
1686-
1687-
if (empty($rowImages)) {
1688+
//Add image to hide to images list if it does not exist
1689+
if (empty($rowImages[self::COL_MEDIA_IMAGE])
1690+
|| !in_array($image, $rowImages[self::COL_MEDIA_IMAGE])
1691+
) {
16881692
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
16891693
}
16901694
}
@@ -1725,24 +1729,29 @@ protected function _saveProducts()
17251729
continue;
17261730
}
17271731

1728-
if (isset($existingImages[$rowSku][$uploadedFile])) {
1729-
$currentFileData = $existingImages[$rowSku][$uploadedFile];
1732+
if (isset($rowExistingImages[$uploadedFile])) {
1733+
$currentFileData = $rowExistingImages[$uploadedFile];
1734+
$currentFileData['store_id'] = $storeId;
1735+
$storeMediaGalleryValueExists = isset($rowStoreMediaGalleryValues[$uploadedFile]);
1736+
if (array_key_exists($uploadedFile, $imageHiddenStates)
1737+
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
1738+
) {
1739+
$imagesForChangeVisibility[] = [
1740+
'disabled' => $imageHiddenStates[$uploadedFile],
1741+
'imageData' => $currentFileData,
1742+
'exists' => $storeMediaGalleryValueExists
1743+
];
1744+
$storeMediaGalleryValueExists = true;
1745+
}
1746+
17301747
if (isset($rowLabels[$column][$columnImageKey])
17311748
&& $rowLabels[$column][$columnImageKey] !=
17321749
$currentFileData['label']
17331750
) {
17341751
$labelsForUpdate[] = [
17351752
'label' => $rowLabels[$column][$columnImageKey],
1736-
'imageData' => $currentFileData
1737-
];
1738-
}
1739-
1740-
if (array_key_exists($uploadedFile, $imageHiddenStates)
1741-
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
1742-
) {
1743-
$imagesForChangeVisibility[] = [
1744-
'disabled' => $imageHiddenStates[$uploadedFile],
1745-
'imageData' => $currentFileData
1753+
'imageData' => $currentFileData,
1754+
'exists' => $storeMediaGalleryValueExists
17461755
];
17471756
}
17481757
} else {

app/code/Magento/CatalogImportExport/Model/Import/Product/MediaGalleryProcessor.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\App\ResourceConnection;
1313
use Magento\Framework\EntityManager\MetadataPool;
1414
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
15-
use Magento\Store\Model\Store;
1615

1716
/**
1817
* Process and saves images during import.
@@ -179,13 +178,15 @@ private function updateMediaGalleryField(array $data, $field)
179178
$insertData = [];
180179
foreach ($data as $datum) {
181180
$imageData = $datum['imageData'];
181+
$exists = $datum['exists'] ?? true;
182182

183-
if ($imageData[$field] === null) {
183+
if (!$exists) {
184184
$insertData[] = [
185185
$field => $datum[$field],
186186
$this->getProductEntityLinkField() => $imageData[$this->getProductEntityLinkField()],
187187
'value_id' => $imageData['value_id'],
188-
'store_id' => Store::DEFAULT_STORE_ID,
188+
'store_id' => $imageData['store_id'],
189+
'position' => $imageData['position'],
189190
];
190191
} else {
191192
$this->connection->update(
@@ -196,7 +197,7 @@ private function updateMediaGalleryField(array $data, $field)
196197
[
197198
$this->getProductEntityLinkField() . ' = ?' => $imageData[$this->getProductEntityLinkField()],
198199
'value_id = ?' => $imageData['value_id'],
199-
'store_id = ?' => Store::DEFAULT_STORE_ID,
200+
'store_id = ?' => $imageData['store_id'],
200201
]
201202
);
202203
}
@@ -240,14 +241,15 @@ public function getExistingImages(array $bunch)
240241
)->joinLeft(
241242
['mgv' => $this->mediaGalleryValueTableName],
242243
sprintf(
243-
'(mg.value_id = mgv.value_id AND mgv.%s = mgvte.%s AND mgv.store_id = %d)',
244+
'(mgv.%s = mgvte.%s AND mg.value_id = mgv.value_id)',
244245
$this->getProductEntityLinkField(),
245-
$this->getProductEntityLinkField(),
246-
Store::DEFAULT_STORE_ID
246+
$this->getProductEntityLinkField()
247247
),
248248
[
249+
'store_id' => 'mgv.store_id',
249250
'label' => 'mgv.label',
250251
'disabled' => 'mgv.disabled',
252+
'position' => 'mgv.position',
251253
]
252254
)->joinInner(
253255
['pe' => $this->productEntityTableName],
@@ -259,7 +261,9 @@ public function getExistingImages(array $bunch)
259261
);
260262

261263
foreach ($this->connection->fetchAll($select) as $image) {
262-
$result[$image['sku']][$image['value']] = $image;
264+
$storeId = $image['store_id'];
265+
unset($image['store_id']);
266+
$result[$storeId][$image['sku']][$image['value']] = $image;
263267
}
264268

265269
return $result;

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Magento\ImportExport\Model\Import;
3030
use Magento\ImportExport\Model\Import\Source\Csv;
3131
use Magento\Store\Model\Store;
32+
use Magento\Store\Model\StoreManagerInterface;
3233
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
3334
use Psr\Log\LoggerInterface;
3435
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
@@ -386,14 +387,14 @@ public function testSaveCustomOptions(string $importFile, string $sku, int $expe
386387
public function testSaveCustomOptionsWithMultipleStoreViews()
387388
{
388389
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
389-
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
390-
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
390+
/** @var StoreManagerInterface $storeManager */
391+
$storeManager = $objectManager->get(StoreManagerInterface::class);
391392
$storeCodes = [
392393
'admin',
393394
'default',
394395
'secondstore',
395396
];
396-
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
397+
/** @var StoreManagerInterface $storeManager */
397398
$importFile = 'product_with_custom_options_and_multiple_store_views.csv';
398399
$sku = 'simple';
399400
$pathToFile = __DIR__ . '/_files/' . $importFile;
@@ -1187,7 +1188,7 @@ public function testProductsWithMultipleStores()
11871188
$product->load($id);
11881189
$this->assertEquals('1', $product->getHasOptions());
11891190

1190-
$objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->setCurrentStore('fixturestore');
1191+
$objectManager->get(StoreManagerInterface::class)->setCurrentStore('fixturestore');
11911192

11921193
/** @var \Magento\Catalog\Model\Product $simpleProduct */
11931194
$simpleProduct = $objectManager->create(\Magento\Catalog\Model\Product::class);
@@ -2246,13 +2247,20 @@ function ($output, $error) {
22462247
* Load product by given product sku
22472248
*
22482249
* @param string $sku
2250+
* @param mixed $store
22492251
* @return \Magento\Catalog\Model\Product
22502252
*/
2251-
private function getProductBySku($sku)
2253+
private function getProductBySku($sku, $store = null)
22522254
{
22532255
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
22542256
$productId = $resource->getIdBySku($sku);
22552257
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
2258+
if ($store) {
2259+
/** @var StoreManagerInterface $storeManager */
2260+
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
2261+
$store = $storeManager->getStore($store);
2262+
$product->setStoreId($store->getId());
2263+
}
22562264
$product->load($productId);
22572265

22582266
return $product;
@@ -2773,4 +2781,56 @@ public function testProductBaseImageAfterImport()
27732781
$productAfterImport = $this->getProductBySku('simple_new');
27742782
$this->assertNotEquals('/no/exists/image/magento_image.jpg', $productAfterImport->getData('image'));
27752783
}
2784+
2785+
/**
2786+
* Tests that images are hidden only for a store view in "store_view_code".
2787+
*
2788+
* @magentoDataFixture mediaImportImageFixture
2789+
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
2790+
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
2791+
*/
2792+
public function testHideImageForStoreView()
2793+
{
2794+
$expectedImageFile = '/m/a/magento_image.jpg';
2795+
$secondStoreCode = 'fixturestore';
2796+
$productSku = 'simple';
2797+
$this->importDataForMediaTest('import_hide_image_for_storeview.csv');
2798+
$product = $this->getProductBySku($productSku);
2799+
$imageItems = $product->getMediaGalleryImages()->getItems();
2800+
$this->assertCount(1, $imageItems);
2801+
$imageItem = array_shift($imageItems);
2802+
$this->assertEquals($expectedImageFile, $imageItem->getFile());
2803+
$product = $this->getProductBySku($productSku, $secondStoreCode);
2804+
$imageItems = $product->getMediaGalleryImages()->getItems();
2805+
$this->assertCount(0, $imageItems);
2806+
}
2807+
2808+
/**
2809+
* Test that images labels are updated only for a store view in "store_view_code".
2810+
*
2811+
* @magentoDataFixture mediaImportImageFixture
2812+
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
2813+
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
2814+
*/
2815+
public function testChangeImageLabelForStoreView()
2816+
{
2817+
$expectedImageFile = '/m/a/magento_image.jpg';
2818+
$expectedLabelForDefaultStoreView = 'Image Alt Text';
2819+
$expectedLabelForSecondStoreView = 'Magento Logo';
2820+
$secondStoreCode = 'fixturestore';
2821+
$productSku = 'simple';
2822+
$this->importDataForMediaTest('import_change_image_label_for_storeview.csv');
2823+
$product = $this->getProductBySku($productSku);
2824+
$imageItems = $product->getMediaGalleryImages()->getItems();
2825+
$this->assertCount(1, $imageItems);
2826+
$imageItem = array_shift($imageItems);
2827+
$this->assertEquals($expectedImageFile, $imageItem->getFile());
2828+
$this->assertEquals($expectedLabelForDefaultStoreView, $imageItem->getLabel());
2829+
$product = $this->getProductBySku($productSku, $secondStoreCode);
2830+
$imageItems = $product->getMediaGalleryImages()->getItems();
2831+
$this->assertCount(1, $imageItems);
2832+
$imageItem = array_shift($imageItems);
2833+
$this->assertEquals($expectedImageFile, $imageItem->getFile());
2834+
$this->assertEquals($expectedLabelForSecondStoreView, $imageItem->getLabel());
2835+
}
27762836
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"sku","store_view_code","base_image", "base_image_label"
2+
"simple","fixturestore","/m/a/magento_image.jpg", "Magento Logo"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"sku","store_view_code","hide_from_product_page"
2+
"simple","fixturestore","/m/a/magento_image.jpg"

0 commit comments

Comments
 (0)