diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product.php b/app/code/Magento/CatalogImportExport/Model/Import/Product.php index 6f8b2248d8d89..0504f0cd3ef41 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product.php @@ -1641,14 +1641,9 @@ protected function _saveProducts() $rowScope = $this->getRowScope($rowData); $urlKey = $this->getUrlKey($rowData); - if (!empty($rowData[self::URL_KEY])) { - // If url_key column and its value were in the CSV file + + if (!empty($urlKey)) { $rowData[self::URL_KEY] = $urlKey; - } else if ($this->isNeedToChangeUrlKey($rowData)) { - // If url_key column was empty or even not declared in the CSV file but by the rules it is need to - // be setteed. In case when url_key is generating from name column we have to ensure that the bunch - // of products will pass for the event with url_key column. - $bunch[$rowNum][self::URL_KEY] = $rowData[self::URL_KEY] = $urlKey; } $rowSku = $rowData[self::COL_SKU]; @@ -2557,7 +2552,9 @@ public function validateRow(array $rowData, $rowNum) */ private function isNeedToValidateUrlKey($rowData) { - return (!empty($rowData[self::URL_KEY]) || !empty($rowData[self::COL_NAME])) + $urlKey = $this->getUrlKey($rowData); + + return (!empty($urlKey)) && (empty($rowData[self::COL_VISIBILITY]) || $rowData[self::COL_VISIBILITY] !== (string)Visibility::getOptionArray()[Visibility::VISIBILITY_NOT_VISIBLE]); @@ -2876,7 +2873,13 @@ protected function getUrlKey($rowData) return $this->productUrl->formatUrlKey($rowData[self::URL_KEY]); } - if (!empty($rowData[self::COL_NAME])) { + /** + * If the product exists, assume it already has a URL Key and even + * if a name is provided in the import data, it should not be used + * to overwrite that existing URL Key the product already has. + */ + $isSkuExist = $this->isSkuExist($rowData[self::COL_SKU]); + if (!$isSkuExist && !empty($rowData[self::COL_NAME])) { return $this->productUrl->formatUrlKey($rowData[self::COL_NAME]); } diff --git a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php index a48b5d43e501b..56d30fa40ce2b 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php @@ -1513,7 +1513,7 @@ public function testProductWithLinks() /** * @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php - * @magentoDbIsolation disabled + * @magentoDbIsolation enabled * @magentoAppIsolation enabled */ public function testExistingProductWithUrlKeys() @@ -1598,10 +1598,15 @@ public function testAddUpdateProductWithInvalidUrlKeys() : void */ public function testImportWithoutUrlKeys() { + /** + * Products `simple1` and `simple2` are created by fixture so already + * have a URL Key, whereas `new-simple` is a new product so the import + * will generate it's URL Key from the name provided in the CSV. + */ $products = [ - 'simple1' => 'simple-1', - 'simple2' => 'simple-2', - 'simple3' => 'simple-3' + 'simple1' => 'url-key', + 'simple2' => 'url-key2', + 'new-simple' => 'new-simple', ]; $filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class); $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); diff --git a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/products_to_import_without_url_keys.csv b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/products_to_import_without_url_keys.csv index ff1b9f4b02afb..aa8f7eefd8239 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/products_to_import_without_url_keys.csv +++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/products_to_import_without_url_keys.csv @@ -2,3 +2,4 @@ sku,product_type,store_view_code,name,price,attribute_set_code,url_key simple1,simple,,"simple 1",25,Default,"" simple2,simple,,"simple 2",34,Default,"" simple3,simple,,"simple 3",58,Default,"" +new-simple,simple,,"new simple",25,Default,"" \ No newline at end of file