Skip to content

Commit 81b24e6

Browse files
author
Dmytro Voskoboinikov
committed
Merge branch 'MAGETWO-73002' into 2.2-bugfixes-280218
2 parents 9e06419 + 82e1951 commit 81b24e6

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

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

Lines changed: 43 additions & 3 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;
@@ -12,6 +13,9 @@
1213
use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap;
1314
use Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer;
1415
use Magento\Framework\Event\ObserverInterface;
16+
use Magento\Store\Model\ResourceModel\Group\CollectionFactory;
17+
use Magento\Store\Model\ResourceModel\Group\Collection as StoreGroupCollection;
18+
use Magento\Framework\App\ObjectManager;
1519

1620
/**
1721
* Generates Category Url Rewrites after save and Products Url Rewrites assigned to the category that's being saved
@@ -43,28 +47,37 @@ class CategoryProcessUrlRewriteSavingObserver implements ObserverInterface
4347
*/
4448
private $dataUrlRewriteClassNames;
4549

50+
/**
51+
* @var CollectionFactory
52+
*/
53+
private $storeGroupFactory;
54+
4655
/**
4756
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
4857
* @param UrlRewriteHandler $urlRewriteHandler
4958
* @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer
5059
* @param DatabaseMapPool $databaseMapPool
5160
* @param string[] $dataUrlRewriteClassNames
61+
* @param CollectionFactory|null $storeGroupFactory
5262
*/
5363
public function __construct(
5464
CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
5565
UrlRewriteHandler $urlRewriteHandler,
5666
UrlRewriteBunchReplacer $urlRewriteBunchReplacer,
5767
DatabaseMapPool $databaseMapPool,
5868
$dataUrlRewriteClassNames = [
59-
DataCategoryUrlRewriteDatabaseMap::class,
60-
DataProductUrlRewriteDatabaseMap::class
61-
]
69+
DataCategoryUrlRewriteDatabaseMap::class,
70+
DataProductUrlRewriteDatabaseMap::class
71+
],
72+
CollectionFactory $storeGroupFactory = null
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,10 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8295
return;
8396
}
8497

98+
if (!$category->hasData('store_id')) {
99+
$this->setCategoryStoreId($category);
100+
}
101+
85102
$mapsGenerated = false;
86103
if ($category->dataHasChangedFor('url_key')
87104
|| $category->dataHasChangedFor('is_anchor')
@@ -102,6 +119,29 @@ public function execute(\Magento\Framework\Event\Observer $observer)
102119
}
103120
}
104121

122+
/**
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")
128+
*
129+
* @param Category $category
130+
* @return void
131+
*/
132+
private function setCategoryStoreId($category)
133+
{
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+
105145
/**
106146
* Resets used data maps to free up memory and temporary tables
107147
*

0 commit comments

Comments
 (0)