Skip to content

Commit 9b2aeaa

Browse files
author
Mastiuhin Olexandr
committed
MAGETWO-95313: [2.3] Missing URL Keys upon product import
1 parent a474225 commit 9b2aeaa

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

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

Lines changed: 31 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,27 @@ protected function getResource()
28892898
return $this->_resource;
28902899
}
28912900

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

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)