Skip to content

Commit 06c7897

Browse files
author
Roman Lytvynenko
committed
MC-19213: Category Specific URL Rewrite is not getting generated while importing and assigning the product to Category
1 parent 74aa9c6 commit 06c7897

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,16 @@ protected function _populateForUrlGeneration($rowData)
299299
*/
300300
private function isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku): bool
301301
{
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)) {
302+
if ((
303+
(empty($newSku) || !isset($newSku['entity_id']))
304+
|| ($this->import->getRowScope($rowData) == ImportProduct::SCOPE_STORE
305+
&& empty($rowData[self::URL_KEY_ATTRIBUTE_CODE]))
306+
|| (array_key_exists(strtolower($rowData[ImportProduct::COL_SKU]), $oldSku)
307+
&& !isset($rowData[self::URL_KEY_ATTRIBUTE_CODE])
308+
&& $this->import->getBehavior() === ImportExport::BEHAVIOR_APPEND)
309+
)
310+
&& !isset($rowData["categories"])
311+
) {
308312
return false;
309313
}
310314
return true;

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Framework\Registry;
2626
use Magento\ImportExport\Model\Import;
2727
use Magento\Store\Model\Store;
28+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
2829
use Psr\Log\LoggerInterface;
2930
use Magento\ImportExport\Model\Import\Source\Csv;
3031

@@ -1647,6 +1648,63 @@ public function testImportWithNonExistingImage()
16471648
}
16481649
}
16491650

1651+
/**
1652+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
1653+
* @magentoDbIsolation enabled
1654+
* @magentoAppIsolation enabled
1655+
*/
1656+
public function testUpdateUrlRewritesOnImport()
1657+
{
1658+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
1659+
1660+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1661+
1662+
$source = $this->objectManager->create(
1663+
\Magento\ImportExport\Model\Import\Source\Csv::class,
1664+
[
1665+
'file' => __DIR__ . '/_files/products_to_import_with_category.csv',
1666+
'directory' => $directory
1667+
]
1668+
);
1669+
$errors = $this->_model->setParameters(
1670+
[
1671+
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
1672+
'entity' => \Magento\Catalog\Model\Product::ENTITY
1673+
]
1674+
)->setSource(
1675+
$source
1676+
)->validateData();
1677+
1678+
$this->assertTrue($errors->getErrorsCount() == 0);
1679+
1680+
$this->_model->importData();
1681+
1682+
/** @var \Magento\Catalog\Model\Product $product */
1683+
$product = $this->objectManager->create(\Magento\Catalog\Model\ProductRepository::class)->get('simple');
1684+
1685+
$repUrlRewriteCol = $this->objectManager->create(
1686+
UrlRewriteCollection::class
1687+
);
1688+
1689+
/** @var UrlRewriteCollection $collUrlRewrite */
1690+
$collUrlRewrite = $repUrlRewriteCol->addFieldToSelect(['request_path'])
1691+
->addFieldToFilter('entity_id', ['eq'=> $product->getEntityId()])
1692+
->addFieldToFilter('entity_type', ['eq'=> 'product'])
1693+
->load();
1694+
1695+
$this->assertCount(2, $collUrlRewrite);
1696+
1697+
$this->assertEquals(
1698+
sprintf('%s.html', $product->getUrlKey()),
1699+
$collUrlRewrite->getFirstItem()->getRequestPath()
1700+
);
1701+
1702+
$this->assertContains(
1703+
sprintf('men/tops/%s.html', $product->getUrlKey()),
1704+
$collUrlRewrite->getLastItem()->getRequestPath()
1705+
);
1706+
}
1707+
16501708
/**
16511709
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
16521710
* @magentoDbIsolation enabled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,store_view_code,attribute_set_code,product_type,categories
2+
simple,default,Default,simple,Default Category/Men/Tops

0 commit comments

Comments
 (0)