Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,""