Skip to content

Commit 468d2ac

Browse files
author
Mastiuhin Olexandr
committed
Merge branch 'MAGETWO-95313' into 2.3-develop-pr10
2 parents c515b40 + 2cfdec7 commit 468d2ac

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,16 @@ protected function _saveProducts()
16401640
}
16411641
$rowScope = $this->getRowScope($rowData);
16421642

1643-
$rowData[self::URL_KEY] = $this->getUrlKey($rowData);
1643+
$urlKey = $this->getUrlKey($rowData);
1644+
if (!empty($rowData[self::URL_KEY])) {
1645+
// If url_key column and its value were in the CSV file
1646+
$rowData[self::URL_KEY] = $urlKey;
1647+
} else if ($this->isNeedToChangeUrlKey($rowData)) {
1648+
// If url_key column was empty or even not declared in the CSV file but by the rules it is need to
1649+
// be setteed. In case when url_key is generating from name column we have to ensure that the bunch
1650+
// of products will pass for the event with url_key column.
1651+
$bunch[$rowNum][self::URL_KEY] = $rowData[self::URL_KEY] = $urlKey;
1652+
}
16441653

16451654
$rowSku = $rowData[self::COL_SKU];
16461655

@@ -2889,6 +2898,26 @@ protected function getResource()
28892898
return $this->_resource;
28902899
}
28912900

2901+
/**
2902+
* Whether a url key is needed to be change.
2903+
*
2904+
* @param array $rowData
2905+
* @return bool
2906+
*/
2907+
private function isNeedToChangeUrlKey(array $rowData): bool
2908+
{
2909+
$urlKey = $this->getUrlKey($rowData);
2910+
$productExists = $this->isSkuExist($rowData[self::COL_SKU]);
2911+
$markedToEraseUrlKey = isset($rowData[self::URL_KEY]);
2912+
// The product isn't new and the url key index wasn't marked for change.
2913+
if (!$urlKey && $productExists && !$markedToEraseUrlKey) {
2914+
// Seems there is no need to change the url key
2915+
return false;
2916+
}
2917+
2918+
return true;
2919+
}
2920+
28922921
/**
28932922
* Get product entity link field
28942923
*

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,45 @@ public function testImportWithoutUrlKeys()
16431643
}
16441644
}
16451645

1646+
/**
1647+
* Make sure the absence of a url_key column in the csv file won't erase the url key of the existing products.
1648+
* To reach the goal we need to not send the name column, as the url key is generated from it.
1649+
*
1650+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
1651+
* @magentoDbIsolation disabled
1652+
* @magentoAppIsolation enabled
1653+
*/
1654+
public function testImportWithoutUrlKeysAndName()
1655+
{
1656+
$products = [
1657+
'simple1' => 'url-key',
1658+
'simple2' => 'url-key2',
1659+
];
1660+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
1661+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1662+
$source = $this->objectManager->create(
1663+
\Magento\ImportExport\Model\Import\Source\Csv::class,
1664+
[
1665+
'file' => __DIR__ . '/_files/products_to_import_without_url_keys_and_name.csv',
1666+
'directory' => $directory
1667+
]
1668+
);
1669+
1670+
$errors = $this->_model->setParameters(
1671+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
1672+
)
1673+
->setSource($source)
1674+
->validateData();
1675+
1676+
$this->assertTrue($errors->getErrorsCount() == 0);
1677+
$this->_model->importData();
1678+
1679+
$productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
1680+
foreach ($products as $productSku => $productUrlKey) {
1681+
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
1682+
}
1683+
}
1684+
16461685
/**
16471686
* @magentoAppIsolation enabled
16481687
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sku,price
2+
simple1,25
3+
simple2,34

0 commit comments

Comments
 (0)