Skip to content

[#18269] If import product has missing categories, make a lookup for existing categories to keep the correct url rewrites #18942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -436,6 +443,8 @@ protected function currentUrlRewritesRegenerate()
}

/**
* Generate URL Rewrites for Autogenerated URL
*
* @param UrlRewrite $url
* @param Category $category
* @return array
Expand Down Expand Up @@ -470,6 +479,8 @@ protected function generateForAutogenerated($url, $category)
}

/**
* Generate url for given category
*
* @param UrlRewrite $url
* @param Category $category
* @return array
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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')
Expand Down