Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite URL Key with blank value #17882

Merged
merged 12 commits into from
Oct 4, 2018
Merged
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
19 changes: 15 additions & 4 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
}

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