Skip to content

Commit 97dec97

Browse files
author
Dmytro Vilchynskyi
committed
MAGETWO-73002: [GITHUB] Issue with Product Import - "Category has not been created. URL key for specified store already exists" #8304
- Static tests are fixed
1 parent b55197d commit 97dec97

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\CatalogUrlRewrite\Observer;
78

89
use Magento\Catalog\Model\Category;
@@ -65,8 +66,8 @@ public function __construct(
6566
UrlRewriteBunchReplacer $urlRewriteBunchReplacer,
6667
DatabaseMapPool $databaseMapPool,
6768
$dataUrlRewriteClassNames = [
68-
DataCategoryUrlRewriteDatabaseMap::class,
69-
DataProductUrlRewriteDatabaseMap::class
69+
DataCategoryUrlRewriteDatabaseMap::class,
70+
DataProductUrlRewriteDatabaseMap::class
7071
],
7172
CollectionFactory $storeGroupFactory = null
7273
) {
@@ -94,21 +95,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
9495
return;
9596
}
9697

97-
/** @var StoreGroupCollection $storeGroupCollection */
98-
$storeGroupCollection = $this->storeGroupFactory->create();
99-
100-
// in case store_id is not set for category then we can assume that it was passed through product import.
101-
// store group must have only one root category, so receiving category's path and checking if one of it parts
102-
// is the root category for store group, we can set default_store_id value from it to category.
103-
// it prevents urls duplication for different stores
104-
// ("Default Category/category/sub" and "Default Category2/category/sub")
10598
if (!$category->hasData('store_id')) {
106-
foreach ($storeGroupCollection as $storeGroup) {
107-
/** @var \Magento\Store\Model\Group $storeGroup */
108-
if (in_array($storeGroup->getRootCategoryId(), explode('/', $category->getPath()))) {
109-
$category->setStoreId($storeGroup->getDefaultStoreId());
110-
}
111-
}
99+
$this->setCategoryStoreIds($category);
112100
}
113101

114102
$mapsGenerated = false;
@@ -132,13 +120,38 @@ public function execute(\Magento\Framework\Event\Observer $observer)
132120
}
133121

134122
/**
135-
* Resets used data maps to free up memory and temporary tables
123+
* in case store_id is not set for category then we can assume that it was passed through product import.
124+
* store group must have only one root category, so receiving category's path and checking if one of it parts
125+
* is the root category for store group, we can set default_store_id value from it to category.
126+
* it prevents urls duplication for different stores
127+
* ("Default Category/category/sub" and "Default Category2/category/sub")
136128
*
137129
* @param Category $category
138130
* @return void
139131
*/
140-
private function resetUrlRewritesDataMaps($category)
132+
private function setCategoryStoreIds($category)
141133
{
134+
/** @var StoreGroupCollection $storeGroupCollection */
135+
$storeGroupCollection = $this->storeGroupFactory->create();
136+
137+
foreach ($storeGroupCollection as $storeGroup) {
138+
/** @var \Magento\Store\Model\Group $storeGroup */
139+
if (in_array($storeGroup->getRootCategoryId(), explode('/', $category->getPath()))) {
140+
$category->setStoreId($storeGroup->getDefaultStoreId());
141+
}
142+
}
143+
}
144+
145+
/**
146+
* Resets used data maps to free up memory and temporary tables
147+
*
148+
* @param Category $category
149+
* @return void
150+
*/
151+
private
152+
function resetUrlRewritesDataMaps(
153+
$category
154+
) {
142155
foreach ($this->dataUrlRewriteClassNames as $className) {
143156
$this->databaseMapPool->resetMap($className, $category->getEntityId());
144157
}

0 commit comments

Comments
 (0)