Skip to content

Commit 8ff967d

Browse files
author
Oleksandr Iegorov
committed
MAGETWO-99927: Product URL rewrites are always regenerated if products are updated via CSV import in default scope
1 parent 5027a80 commit 8ff967d

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,12 @@ public function validateRow(array $rowData, $rowNum)
24872487
*/
24882488
private function isNeedToValidateUrlKey($rowData)
24892489
{
2490+
if (!empty($rowData[self::COL_SKU]) && empty($rowData[self::URL_KEY])
2491+
&& $this->getBehavior() === Import::BEHAVIOR_APPEND
2492+
&& $this->isSkuExist($rowData[self::COL_SKU])) {
2493+
return false;
2494+
}
2495+
24902496
return (!empty($rowData[self::URL_KEY]) || !empty($rowData[self::COL_NAME]))
24912497
&& (empty($rowData[self::COL_VISIBILITY])
24922498
|| $rowData[self::COL_VISIBILITY]
@@ -2810,7 +2816,8 @@ protected function getUrlKey($rowData)
28102816
return trim(strtolower($urlKey));
28112817
}
28122818

2813-
if (!empty($rowData[self::COL_NAME])) {
2819+
if (!empty($rowData[self::COL_NAME])
2820+
&& (array_key_exists(self::URL_KEY, $rowData) || !$this->isSkuExist($rowData[self::COL_SKU]))) {
28142821
return $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
28152822
}
28162823

app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,8 @@ public function execute(Observer $observer)
255255
protected function _populateForUrlGeneration($rowData)
256256
{
257257
$newSku = $this->import->getNewSku($rowData[ImportProduct::COL_SKU]);
258-
if (empty($newSku) || !isset($newSku['entity_id'])) {
259-
return null;
260-
}
261-
if ($this->import->getRowScope($rowData) == ImportProduct::SCOPE_STORE
262-
&& empty($rowData[self::URL_KEY_ATTRIBUTE_CODE])) {
258+
$oldSku = $this->import->getOldSku();
259+
if (!$this->isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku)) {
263260
return null;
264261
}
265262
$rowData['entity_id'] = $newSku['entity_id'];
@@ -292,6 +289,27 @@ protected function _populateForUrlGeneration($rowData)
292289
return $this;
293290
}
294291

292+
/**
293+
* Check is need to populate data for url generation
294+
*
295+
* @param array $rowData
296+
* @param array $newSku
297+
* @param array $oldSku
298+
* @return bool
299+
*/
300+
private function isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku): bool
301+
{
302+
if ((empty($newSku) || !isset($newSku['entity_id']))
303+
|| ($this->import->getRowScope($rowData) == ImportProduct::SCOPE_STORE
304+
&& empty($rowData[self::URL_KEY_ATTRIBUTE_CODE]))
305+
|| (array_key_exists($rowData[ImportProduct::COL_SKU], $oldSku)
306+
&& !isset($rowData[self::URL_KEY_ATTRIBUTE_CODE])
307+
&& $this->import->getBehavior() === ImportExport::BEHAVIOR_APPEND)) {
308+
return false;
309+
}
310+
return true;
311+
}
312+
295313
/**
296314
* Add store id to product data.
297315
*

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Store\Model\Store;
2727
use Psr\Log\LoggerInterface;
2828
use Magento\Framework\Exception\NoSuchEntityException;
29+
use Magento\ImportExport\Model\Import\Source\Csv;
2930

3031
/**
3132
* Class ProductTest
@@ -1643,6 +1644,35 @@ public function testImportWithNonExistingImage()
16431644
}
16441645
}
16451646

1647+
/**
1648+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
1649+
* @magentoDbIsolation enabled
1650+
* @magentoAppIsolation enabled
1651+
*/
1652+
public function testImportWithoutChangingUrlKeys()
1653+
{
1654+
$filesystem = $this->objectManager->create(Filesystem::class);
1655+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1656+
$source = $this->objectManager->create(
1657+
Csv::class,
1658+
[
1659+
'file' => __DIR__ . '/_files/products_to_import_without_url_key_column.csv',
1660+
'directory' => $directory
1661+
]
1662+
);
1663+
1664+
$errors = $this->_model->setParameters(
1665+
['behavior' => Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
1666+
)
1667+
->setSource($source)
1668+
->validateData();
1669+
1670+
$this->assertTrue($errors->getErrorsCount() == 0);
1671+
$this->_model->importData();
1672+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
1673+
$this->assertEquals('url-key', $productRepository->get('simple1')->getUrlKey());
1674+
}
1675+
16461676
/**
16471677
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
16481678
* @magentoDbIsolation disabled
@@ -2168,7 +2198,6 @@ public function testImportWithFilesystemImages()
21682198
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
21692199
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
21702200
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
2171-
*
21722201
*/
21732202
public function testImportDataChangeAttributeSet()
21742203
{
@@ -2186,7 +2215,7 @@ public function testImportDataChangeAttributeSet()
21862215
);
21872216
$errors = $this->_model->setParameters(
21882217
[
2189-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE,
2218+
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
21902219
'entity' => \Magento\Catalog\Model\Product::ENTITY
21912220
]
21922221
)->setSource(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,product_type,store_view_code,name,price,attribute_set_code
2+
simple1,simple,,"simple 1",25,Default

0 commit comments

Comments
 (0)