Skip to content

Commit 643e6c4

Browse files
author
Oleksandr Dubovyk
committed
Merge remote-tracking branch 'tango/MAGETWO-99927' into pr_23_shtdwn
2 parents f77ac3c + 6e29eaf commit 643e6c4

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @SuppressWarnings(PHPMD.TooManyFields)
3939
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
4040
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
4142
* @since 100.0.2
4243
*/
4344
class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
@@ -1508,6 +1509,7 @@ public function getImagesFromRow(array $rowData)
15081509
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
15091510
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
15101511
* @throws LocalizedException
1512+
* phpcs:disable Generic.Metrics.NestingLevel
15111513
*/
15121514
protected function _saveProducts()
15131515
{
@@ -2487,6 +2489,12 @@ public function validateRow(array $rowData, $rowNum)
24872489
*/
24882490
private function isNeedToValidateUrlKey($rowData)
24892491
{
2492+
if (!empty($rowData[self::COL_SKU]) && empty($rowData[self::URL_KEY])
2493+
&& $this->getBehavior() === Import::BEHAVIOR_APPEND
2494+
&& $this->isSkuExist($rowData[self::COL_SKU])) {
2495+
return false;
2496+
}
2497+
24902498
return (!empty($rowData[self::URL_KEY]) || !empty($rowData[self::COL_NAME]))
24912499
&& (empty($rowData[self::COL_VISIBILITY])
24922500
|| $rowData[self::COL_VISIBILITY]
@@ -2810,7 +2818,8 @@ protected function getUrlKey($rowData)
28102818
return trim(strtolower($urlKey));
28112819
}
28122820

2813-
if (!empty($rowData[self::COL_NAME])) {
2821+
if (!empty($rowData[self::COL_NAME])
2822+
&& (array_key_exists(self::URL_KEY, $rowData) || !$this->isSkuExist($rowData[self::COL_SKU]))) {
28142823
return $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
28152824
}
28162825

app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,8 @@ public function execute(Observer $observer)
255255
protected function _populateForUrlGeneration($rowData)
256256
{
257257
$newSku = $this->import->getNewSku($rowData[ImportProduct::COL_SKU]);
258-
if (empty($newSku) || !isset($newSku['entity_id'])) {
259-
return null;
260-
}
261-
if ($this->import->getRowScope($rowData) == ImportProduct::SCOPE_STORE
262-
&& empty($rowData[self::URL_KEY_ATTRIBUTE_CODE])) {
258+
$oldSku = $this->import->getOldSku();
259+
if (!$this->isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku)) {
263260
return null;
264261
}
265262
$rowData['entity_id'] = $newSku['entity_id'];
@@ -292,6 +289,27 @@ protected function _populateForUrlGeneration($rowData)
292289
return $this;
293290
}
294291

292+
/**
293+
* Check is need to populate data for url generation
294+
*
295+
* @param array $rowData
296+
* @param array $newSku
297+
* @param array $oldSku
298+
* @return bool
299+
*/
300+
private function isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku): bool
301+
{
302+
if ((empty($newSku) || !isset($newSku['entity_id']))
303+
|| ($this->import->getRowScope($rowData) == ImportProduct::SCOPE_STORE
304+
&& empty($rowData[self::URL_KEY_ATTRIBUTE_CODE]))
305+
|| (array_key_exists($rowData[ImportProduct::COL_SKU], $oldSku)
306+
&& !isset($rowData[self::URL_KEY_ATTRIBUTE_CODE])
307+
&& $this->import->getBehavior() === ImportExport::BEHAVIOR_APPEND)) {
308+
return false;
309+
}
310+
return true;
311+
}
312+
295313
/**
296314
* Add store id to product data.
297315
*

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\ImportExport\Model\Import;
2727
use Magento\Store\Model\Store;
2828
use Psr\Log\LoggerInterface;
29+
use Magento\ImportExport\Model\Import\Source\Csv;
2930

3031
/**
3132
* Class ProductTest
@@ -36,6 +37,7 @@
3637
* @magentoDataFixtureBeforeTransaction Magento/Catalog/_files/enable_catalog_product_reindex_schedule.php
3738
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3839
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
40+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
3941
* phpcs:disable Generic.PHP.NoSilencedErrors, Generic.Metrics.NestingLevel, Magento2.Functions.StaticFunction
4042
*/
4143
class ProductTest extends \Magento\TestFramework\Indexer\TestCase
@@ -1645,6 +1647,35 @@ public function testImportWithNonExistingImage()
16451647
}
16461648
}
16471649

1650+
/**
1651+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
1652+
* @magentoDbIsolation enabled
1653+
* @magentoAppIsolation enabled
1654+
*/
1655+
public function testImportWithoutChangingUrlKeys()
1656+
{
1657+
$filesystem = $this->objectManager->create(Filesystem::class);
1658+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1659+
$source = $this->objectManager->create(
1660+
Csv::class,
1661+
[
1662+
'file' => __DIR__ . '/_files/products_to_import_without_url_key_column.csv',
1663+
'directory' => $directory
1664+
]
1665+
);
1666+
1667+
$errors = $this->_model->setParameters(
1668+
['behavior' => Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
1669+
)
1670+
->setSource($source)
1671+
->validateData();
1672+
1673+
$this->assertTrue($errors->getErrorsCount() == 0);
1674+
$this->_model->importData();
1675+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
1676+
$this->assertEquals('url-key', $productRepository->get('simple1')->getUrlKey());
1677+
}
1678+
16481679
/**
16491680
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
16501681
* @magentoDbIsolation disabled
@@ -2170,7 +2201,6 @@ public function testImportWithFilesystemImages()
21702201
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
21712202
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
21722203
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
2173-
*
21742204
*/
21752205
public function testImportDataChangeAttributeSet()
21762206
{
@@ -2188,7 +2218,7 @@ public function testImportDataChangeAttributeSet()
21882218
);
21892219
$errors = $this->_model->setParameters(
21902220
[
2191-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE,
2221+
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
21922222
'entity' => \Magento\Catalog\Model\Product::ENTITY
21932223
]
21942224
)->setSource(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,product_type,store_view_code,name,price,attribute_set_code
2+
simple1,simple,,"simple 1",25,Default

0 commit comments

Comments
 (0)