3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \CatalogUrlRewrite \Observer ;
7
8
8
9
use Magento \Catalog \Model \Category ;
12
13
use Magento \CatalogUrlRewrite \Model \Map \DataProductUrlRewriteDatabaseMap ;
13
14
use Magento \CatalogUrlRewrite \Model \UrlRewriteBunchReplacer ;
14
15
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 ;
15
19
16
20
/**
17
21
* 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
43
47
*/
44
48
private $ dataUrlRewriteClassNames ;
45
49
50
+ /**
51
+ * @var CollectionFactory
52
+ */
53
+ private $ storeGroupFactory ;
54
+
46
55
/**
47
56
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
48
57
* @param UrlRewriteHandler $urlRewriteHandler
49
58
* @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer
50
59
* @param DatabaseMapPool $databaseMapPool
51
60
* @param string[] $dataUrlRewriteClassNames
61
+ * @param CollectionFactory|null $storeGroupFactory
52
62
*/
53
63
public function __construct (
54
64
CategoryUrlRewriteGenerator $ categoryUrlRewriteGenerator ,
55
65
UrlRewriteHandler $ urlRewriteHandler ,
56
66
UrlRewriteBunchReplacer $ urlRewriteBunchReplacer ,
57
67
DatabaseMapPool $ databaseMapPool ,
58
68
$ dataUrlRewriteClassNames = [
59
- DataCategoryUrlRewriteDatabaseMap::class,
60
- DataProductUrlRewriteDatabaseMap::class
61
- ]
69
+ DataCategoryUrlRewriteDatabaseMap::class,
70
+ DataProductUrlRewriteDatabaseMap::class
71
+ ],
72
+ CollectionFactory $ storeGroupFactory = null
62
73
) {
63
74
$ this ->categoryUrlRewriteGenerator = $ categoryUrlRewriteGenerator ;
64
75
$ this ->urlRewriteHandler = $ urlRewriteHandler ;
65
76
$ this ->urlRewriteBunchReplacer = $ urlRewriteBunchReplacer ;
66
77
$ this ->databaseMapPool = $ databaseMapPool ;
67
78
$ this ->dataUrlRewriteClassNames = $ dataUrlRewriteClassNames ;
79
+ $ this ->storeGroupFactory = $ storeGroupFactory
80
+ ?: ObjectManager::getInstance ()->get (CollectionFactory::class);
68
81
}
69
82
70
83
/**
@@ -82,6 +95,10 @@ public function execute(\Magento\Framework\Event\Observer $observer)
82
95
return ;
83
96
}
84
97
98
+ if (!$ category ->hasData ('store_id ' )) {
99
+ $ this ->setCategoryStoreId ($ category );
100
+ }
101
+
85
102
$ mapsGenerated = false ;
86
103
if ($ category ->dataHasChangedFor ('url_key ' )
87
104
|| $ category ->dataHasChangedFor ('is_anchor ' )
@@ -102,6 +119,29 @@ public function execute(\Magento\Framework\Event\Observer $observer)
102
119
}
103
120
}
104
121
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
+
105
145
/**
106
146
* Resets used data maps to free up memory and temporary tables
107
147
*
0 commit comments