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 ;
@@ -65,8 +66,8 @@ public function __construct(
65
66
UrlRewriteBunchReplacer $ urlRewriteBunchReplacer ,
66
67
DatabaseMapPool $ databaseMapPool ,
67
68
$ dataUrlRewriteClassNames = [
68
- DataCategoryUrlRewriteDatabaseMap::class,
69
- DataProductUrlRewriteDatabaseMap::class
69
+ DataCategoryUrlRewriteDatabaseMap::class,
70
+ DataProductUrlRewriteDatabaseMap::class
70
71
],
71
72
CollectionFactory $ storeGroupFactory = null
72
73
) {
@@ -94,21 +95,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
94
95
return ;
95
96
}
96
97
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")
105
98
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 );
112
100
}
113
101
114
102
$ mapsGenerated = false ;
@@ -132,13 +120,38 @@ public function execute(\Magento\Framework\Event\Observer $observer)
132
120
}
133
121
134
122
/**
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")
136
128
*
137
129
* @param Category $category
138
130
* @return void
139
131
*/
140
- private function resetUrlRewritesDataMaps ($ category )
132
+ private function setCategoryStoreIds ($ category )
141
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
+
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
+ ) {
142
155
foreach ($ this ->dataUrlRewriteClassNames as $ className ) {
143
156
$ this ->databaseMapPool ->resetMap ($ className , $ category ->getEntityId ());
144
157
}
0 commit comments