Skip to content

Commit a2f6bff

Browse files
committed
MAGETWO-73002: [GITHUB] Issue with Product Import - "Category has not been created. URL key for specified store already exists" #8304
1 parent 79463a5 commit a2f6bff

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap;
1313
use Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer;
1414
use Magento\Framework\Event\ObserverInterface;
15+
use Magento\Store\Model\ResourceModel\Group\CollectionFactory;
16+
use Magento\Store\Model\ResourceModel\Group\Collection as StoreGroupCollection;
17+
use Magento\Framework\App\ObjectManager;
1518

1619
/**
1720
* Generates Category Url Rewrites after save and Products Url Rewrites assigned to the category that's being saved
@@ -43,12 +46,18 @@ class CategoryProcessUrlRewriteSavingObserver implements ObserverInterface
4346
*/
4447
private $dataUrlRewriteClassNames;
4548

49+
/**
50+
* @var CollectionFactory
51+
*/
52+
private $storeGroupFactory;
53+
4654
/**
4755
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
4856
* @param UrlRewriteHandler $urlRewriteHandler
4957
* @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer
5058
* @param DatabaseMapPool $databaseMapPool
5159
* @param string[] $dataUrlRewriteClassNames
60+
* @param CollectionFactory|null $storeGroupFactory
5261
*/
5362
public function __construct(
5463
CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
@@ -58,13 +67,17 @@ public function __construct(
5867
$dataUrlRewriteClassNames = [
5968
DataCategoryUrlRewriteDatabaseMap::class,
6069
DataProductUrlRewriteDatabaseMap::class
61-
]
70+
],
71+
CollectionFactory $storeGroupFactory = null
72+
6273
) {
6374
$this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator;
6475
$this->urlRewriteHandler = $urlRewriteHandler;
6576
$this->urlRewriteBunchReplacer = $urlRewriteBunchReplacer;
6677
$this->databaseMapPool = $databaseMapPool;
6778
$this->dataUrlRewriteClassNames = $dataUrlRewriteClassNames;
79+
$this->storeGroupFactory = $storeGroupFactory
80+
?: ObjectManager::getInstance()->get(CollectionFactory::class);;
6881
}
6982

7083
/**
@@ -82,6 +95,23 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8295
return;
8396
}
8497

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

0 commit comments

Comments
 (0)