diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 022a78be00197..22ba3b0bc5244 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -221,6 +221,7 @@ public function execute(Observer $observer) /** * Create product model from imported data for URL rewrite purposes. + * Also make a lookup for existing categories for this product if no categories are provided. * * @param array $rowData * @@ -248,7 +249,13 @@ protected function _populateForUrlGeneration($rowData) } } - $this->categoryCache[$rowData['entity_id']] = $this->import->getProductCategories($rowData['sku']); + // If no categories are provided get the actual categories for product to prevent loss of existing rewrites + $productCategoryCache = $this->import->getProductCategories($rowData['sku']); + if (!isset($rowData['categories']) && !$productCategoryCache) { + $productCategoryCache = $product->getCategoryIds(); + } + + $this->categoryCache[$rowData['entity_id']] = $productCategoryCache; $this->websiteCache[$rowData['entity_id']] = $this->import->getProductWebsites($rowData['sku']); foreach ($this->websiteCache[$rowData['entity_id']] as $websiteId) { if (!isset($this->websitesToStoreIds[$websiteId])) { @@ -436,6 +443,8 @@ protected function currentUrlRewritesRegenerate() } /** + * Generate URL Rewrites for Autogenerated URL + * * @param UrlRewrite $url * @param Category $category * @return array @@ -470,6 +479,8 @@ protected function generateForAutogenerated($url, $category) } /** + * Generate url for given category + * * @param UrlRewrite $url * @param Category $category * @return array @@ -503,6 +514,8 @@ protected function generateForCustom($url, $category) } /** + * Loads the category by url just from given metadata + * * @param UrlRewrite $url * @return Category|null|bool */ @@ -517,6 +530,8 @@ protected function retrieveCategoryFromMetadata($url) } /** + * Check whether Category is proper for generation or not + * * @param \Magento\Catalog\Model\Category $category * @param int $storeId * @return bool diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php index fd9ab10537f1c..dcd26ebe50b0a 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php @@ -315,12 +315,14 @@ public function testAfterImportData() ->expects($this->exactly(1)) ->method('getStoreIdByCode') ->will($this->returnValueMap($map)); + $product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [ 'getId', 'setId', 'getSku', 'setStoreId', 'getStoreId', + 'getCategoryIds' ]); $product ->expects($this->exactly($productsCount)) @@ -338,6 +340,10 @@ public function testAfterImportData() $newSku[1]['entity_id'], $newSku[1]['entity_id'] ); + $product + ->expects($this->any()) + ->method('getCategoryIds') + ->willReturn([]); $product ->expects($this->exactly($productsCount)) ->method('getSku')