11
11
use Magento \CatalogUrlRewrite \Model \Category \CurrentUrlRewritesRegenerator ;
12
12
use Magento \CatalogUrlRewrite \Service \V1 \StoreViewService ;
13
13
use Magento \Store \Model \Store ;
14
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
14
15
15
16
class CategoryUrlRewriteGenerator
16
17
{
@@ -32,30 +33,39 @@ class CategoryUrlRewriteGenerator
32
33
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */
33
34
protected $ childrenUrlRewriteGenerator ;
34
35
36
+ /**
37
+ * @var bool
38
+ */
39
+ protected $ overrideStoreUrls ;
40
+
35
41
/**
36
42
* @param \Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator
37
43
* @param \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator
38
44
* @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator
39
45
* @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService
46
+ * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
40
47
*/
41
48
public function __construct (
42
49
CanonicalUrlRewriteGenerator $ canonicalUrlRewriteGenerator ,
43
50
CurrentUrlRewritesRegenerator $ currentUrlRewritesRegenerator ,
44
51
ChildrenUrlRewriteGenerator $ childrenUrlRewriteGenerator ,
45
- StoreViewService $ storeViewService
52
+ StoreViewService $ storeViewService ,
53
+ CategoryRepositoryInterface $ categoryRepository
46
54
) {
47
55
$ this ->storeViewService = $ storeViewService ;
48
56
$ this ->canonicalUrlRewriteGenerator = $ canonicalUrlRewriteGenerator ;
49
57
$ this ->childrenUrlRewriteGenerator = $ childrenUrlRewriteGenerator ;
50
58
$ this ->currentUrlRewritesRegenerator = $ currentUrlRewritesRegenerator ;
59
+ $ this ->categoryRepository = $ categoryRepository ;
51
60
}
52
61
53
62
/**
54
63
* {@inheritdoc}
55
64
*/
56
- public function generate ($ category )
65
+ public function generate ($ category, $ overrideStoreUrls = false )
57
66
{
58
67
$ this ->category = $ category ;
68
+ $ this ->overrideStoreUrls = $ overrideStoreUrls ;
59
69
60
70
$ storeId = $ this ->category ->getStoreId ();
61
71
$ urls = $ this ->isGlobalScope ($ storeId )
@@ -75,16 +85,44 @@ protected function generateForGlobalScope()
75
85
{
76
86
$ urls = [];
77
87
$ categoryId = $ this ->category ->getId ();
78
- foreach ($ this ->category ->getStoreIds () as $ id ) {
79
- if (!$ this ->isGlobalScope ($ id )
80
- && ! $ this ->storeViewService -> doesEntityHaveOverriddenUrlKeyForStore ( $ id , $ categoryId, Category:: ENTITY )
88
+ foreach ($ this ->category ->getStoreIds () as $ storeId ) {
89
+ if (!$ this ->isGlobalScope ($ storeId )
90
+ && $ this ->isOverrideUrlsForStore ( $ storeId , $ categoryId )
81
91
) {
82
- $ urls = array_merge ($ urls , $ this ->generateForSpecificStoreView ($ id ));
92
+ $ this ->updateCategoryUrlForStore ($ storeId );
93
+ $ urls = array_merge ($ urls , $ this ->generateForSpecificStoreView ($ storeId ));
83
94
}
84
95
}
85
96
return $ urls ;
86
97
}
87
98
99
+ /**
100
+ * @param int $storeId
101
+ * @param int $categoryId
102
+ * @return bool
103
+ */
104
+ protected function isOverrideUrlsForStore ($ storeId , $ categoryId )
105
+ {
106
+ return $ this ->overrideStoreUrls
107
+ || !$ this ->storeViewService ->doesEntityHaveOverriddenUrlKeyForStore (
108
+ $ storeId ,
109
+ $ categoryId ,
110
+ Category::ENTITY
111
+ );
112
+ }
113
+
114
+ /**
115
+ * Override url key and url path for category in specific Store
116
+ *
117
+ * @param int $storeId
118
+ * @return void
119
+ */
120
+ protected function updateCategoryUrlForStore ($ storeId )
121
+ {
122
+ $ category = $ this ->categoryRepository ->get ($ this ->category ->getId (), $ storeId );
123
+ $ this ->category ->addData (['url_key ' => $ category ->getUrlKey (), 'url_path ' => $ category ->getUrlPath ()]);
124
+ }
125
+
88
126
/**
89
127
* Check is global scope
90
128
*
0 commit comments