8
8
use Magento \Catalog \Model \Category ;
9
9
use Magento \Catalog \Model \CategoryRepository ;
10
10
use Magento \Catalog \Model \Product ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
11
12
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
12
13
use Magento \UrlRewrite \Model \OptionProvider ;
13
14
use Magento \CatalogUrlRewrite \Model \ObjectRegistry ;
18
19
use Magento \CatalogUrlRewrite \Model \Map \UrlRewriteFinder ;
19
20
use Magento \Framework \App \ObjectManager ;
20
21
use Magento \UrlRewrite \Model \MergeDataProviderFactory ;
22
+ use Magento \CatalogUrlRewrite \Block \UrlKeyRenderer ;
23
+ use Magento \Store \Model \ScopeInterface ;
21
24
22
25
/**
23
26
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -27,18 +30,21 @@ class CurrentUrlRewritesRegenerator
27
30
/**
28
31
* @var Product
29
32
* @deprecated 100.1.0
33
+ * @see not used
30
34
*/
31
35
protected $ product ;
32
36
33
37
/**
34
38
* @var ObjectRegistry
35
39
* @deprecated 100.1.0
40
+ * @see not used
36
41
*/
37
42
protected $ productCategories ;
38
43
39
44
/**
40
45
* @var UrlFinderInterface
41
46
* @deprecated 100.1.0
47
+ * @see not used
42
48
*/
43
49
protected $ urlFinder ;
44
50
@@ -72,21 +78,28 @@ class CurrentUrlRewritesRegenerator
72
78
*/
73
79
private $ categoryRepository ;
74
80
81
+ /**
82
+ * @var ScopeConfigInterface
83
+ */
84
+ private ScopeConfigInterface $ scopeConfig ;
85
+
75
86
/**
76
87
* @param UrlFinderInterface $urlFinder
77
88
* @param ProductUrlPathGenerator $productUrlPathGenerator
78
89
* @param UrlRewriteFactory $urlRewriteFactory
79
90
* @param UrlRewriteFinder|null $urlRewriteFinder
80
91
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
81
92
* @param CategoryRepository|null $categoryRepository
93
+ * @param ScopeConfigInterface|null $scopeConfig
82
94
*/
83
95
public function __construct (
84
96
UrlFinderInterface $ urlFinder ,
85
97
ProductUrlPathGenerator $ productUrlPathGenerator ,
86
98
UrlRewriteFactory $ urlRewriteFactory ,
87
99
?UrlRewriteFinder $ urlRewriteFinder = null ,
88
100
?MergeDataProviderFactory $ mergeDataProviderFactory = null ,
89
- ?CategoryRepository $ categoryRepository = null
101
+ ?CategoryRepository $ categoryRepository = null ,
102
+ ?ScopeConfigInterface $ scopeConfig = null
90
103
) {
91
104
$ this ->urlFinder = $ urlFinder ;
92
105
$ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -98,6 +111,7 @@ public function __construct(
98
111
}
99
112
$ this ->categoryRepository = $ categoryRepository ?: ObjectManager::getInstance ()->get (CategoryRepository::class);
100
113
$ this ->mergeDataProviderPrototype = $ mergeDataProviderFactory ->create ();
114
+ $ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
101
115
}
102
116
103
117
/**
@@ -119,6 +133,14 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate
119
133
$ rootCategoryId
120
134
);
121
135
136
+ $ isSaveHistory = $ this ->scopeConfig ->isSetFlag (
137
+ UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY ,
138
+ ScopeInterface::SCOPE_STORE ,
139
+ $ storeId
140
+ );
141
+
142
+ $ product ->setData ('save_rewrites_history ' , $ isSaveHistory );
143
+
122
144
foreach ($ currentUrlRewrites as $ currentUrlRewrite ) {
123
145
$ category = $ this ->retrieveCategoryFromMetadata ($ currentUrlRewrite , $ productCategories );
124
146
if ($ category === false ) {
@@ -158,10 +180,12 @@ public function generateAnchor(
158
180
ProductUrlRewriteGenerator::ENTITY_TYPE ,
159
181
$ rootCategoryId
160
182
);
161
-
162
- foreach ($ productCategories ->getList () as $ productCategory ) {
163
- $ anchorCategoryIds = array_merge ($ productCategory ->getAnchorsAbove (), $ anchorCategoryIds );
164
- }
183
+ $ anchorCategoryIds = array_merge (
184
+ ...array_map (
185
+ fn ($ productCategory ) => $ productCategory ->getAnchorsAbove (),
186
+ $ productCategories ->getList ()
187
+ )
188
+ );
165
189
166
190
foreach ($ currentUrlRewrites as $ currentUrlRewrite ) {
167
191
$ metadata = $ currentUrlRewrite ->getMetadata ();
@@ -181,6 +205,8 @@ public function generateAnchor(
181
205
}
182
206
183
207
/**
208
+ * Generate URL rewrites for autogenerated URLs
209
+ *
184
210
* @param UrlRewrite $url
185
211
* @param int $storeId
186
212
* @param Category|null $category
@@ -209,6 +235,8 @@ protected function generateForAutogenerated($url, $storeId, $category, $product
209
235
}
210
236
211
237
/**
238
+ * Generate URL rewrites for custom URLs
239
+ *
212
240
* @param UrlRewrite $url
213
241
* @param int $storeId
214
242
* @param Category|null $category
@@ -237,6 +265,8 @@ protected function generateForCustom($url, $storeId, $category, $product = null)
237
265
}
238
266
239
267
/**
268
+ * Retrieve category from URL metadata
269
+ *
240
270
* @param UrlRewrite $url
241
271
* @param ObjectRegistry|null $productCategories
242
272
* @return Category|null|bool
0 commit comments