diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product.php b/app/code/Magento/CatalogImportExport/Model/Import/Product.php index 743bcddede9c3..96f187f03612b 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product.php @@ -1561,7 +1561,10 @@ protected function _saveProducts() } $rowScope = $this->getRowScope($rowData); - $rowData[self::URL_KEY] = $this->getUrlKey($rowData); + $urlKey = $this->getUrlKey($rowData); + if (!empty($urlKey)) { + $rowData[self::URL_KEY] = $urlKey; + } $rowSku = $rowData[self::COL_SKU]; @@ -2406,7 +2409,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]); @@ -2718,8 +2723,14 @@ protected function getUrlKey($rowData) if (!empty($rowData[self::URL_KEY])) { return strtolower($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 0251c2b4c34cb..dbc8619e6e678 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php @@ -1568,15 +1568,20 @@ public function testExistingProductWithUrlKeys() /** * @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php - * @magentoDbIsolation disabled + * @magentoDbIsolation enabled * @magentoAppIsolation enabled */ 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..80898c5440df7 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,""