12
12
use Magento \CatalogUrlRewrite \Model \Map \DataProductUrlRewriteDatabaseMap ;
13
13
use Magento \CatalogUrlRewrite \Model \UrlRewriteBunchReplacer ;
14
14
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 ;
15
18
16
19
/**
17
20
* 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
43
46
*/
44
47
private $ dataUrlRewriteClassNames ;
45
48
49
+ /**
50
+ * @var CollectionFactory
51
+ */
52
+ private $ storeGroupFactory ;
53
+
46
54
/**
47
55
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
48
56
* @param UrlRewriteHandler $urlRewriteHandler
49
57
* @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer
50
58
* @param DatabaseMapPool $databaseMapPool
51
59
* @param string[] $dataUrlRewriteClassNames
60
+ * @param CollectionFactory|null $storeGroupFactory
52
61
*/
53
62
public function __construct (
54
63
CategoryUrlRewriteGenerator $ categoryUrlRewriteGenerator ,
@@ -58,13 +67,17 @@ public function __construct(
58
67
$ dataUrlRewriteClassNames = [
59
68
DataCategoryUrlRewriteDatabaseMap::class,
60
69
DataProductUrlRewriteDatabaseMap::class
61
- ]
70
+ ],
71
+ CollectionFactory $ storeGroupFactory = null
72
+
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,23 @@ public function execute(\Magento\Framework\Event\Observer $observer)
82
95
return ;
83
96
}
84
97
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
+
85
115
$ mapsGenerated = false ;
86
116
if ($ category ->dataHasChangedFor ('url_key ' )
87
117
|| $ category ->dataHasChangedFor ('is_anchor ' )
0 commit comments