@@ -66,7 +66,7 @@ public function __construct(
66
66
\Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
67
67
\Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator $ cmsPageUrlPathGenerator ,
68
68
UrlFinderInterface $ urlFinder ,
69
- ?AppendUrlRewritesToProducts $ productAppendRewrites = null
69
+ ?AppendUrlRewritesToProducts $ productAppendRewrites = null
70
70
) {
71
71
parent ::__construct ($ context );
72
72
$ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -82,7 +82,7 @@ public function __construct(
82
82
*
83
83
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
84
84
* @return void
85
- * @throws \Magento\Framework\Exception\ LocalizedException
85
+ * @throws LocalizedException
86
86
*/
87
87
protected function _handleCatalogUrlRewrite ($ model )
88
88
{
@@ -96,17 +96,17 @@ protected function _handleCatalogUrlRewrite($model)
96
96
$ model ->setMetadata (['category_id ' => $ categoryId ]);
97
97
}
98
98
}
99
- $ this ->generateTargetPath ($ model );
99
+ $ model -> setTargetPath ( $ this ->generateTargetPath ($ model) );
100
100
}
101
101
}
102
102
103
103
/**
104
104
* Generate Target Path
105
105
*
106
106
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
107
- * @throws \Magento\Framework\Exception\ LocalizedException
107
+ * @throws LocalizedException
108
108
*/
109
- protected function generateTargetPath ($ model )
109
+ protected function generateTargetPath ($ model ): ? string
110
110
{
111
111
$ targetPath = $ this ->getCanonicalTargetPath ();
112
112
if ($ model ->getRedirectType () && !$ model ->getIsAutogenerated ()) {
@@ -118,46 +118,109 @@ protected function generateTargetPath($model)
118
118
])) {
119
119
$ targetPath = $ rewrite ->getRequestPath ();
120
120
} else {
121
- $ check = $ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ?
122
- $ this ->_getProduct ()->canBeShowInCategory ($ this ->_getCategory ()->getId ()) &&
123
- in_array ($ model ->getStoreId (), $ this ->_getProduct ()->getStoreIds ()) :
124
- $ this ->_getCategory ()->getStoreId () == $ model ->getStoreId ();
125
- if (false === $ check ) {
126
- throw new LocalizedException (
127
- $ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT
128
- ? __ ("The selected product isn't associated with the selected store or category. " )
129
- : __ ("The selected category isn't associated with the selected store. " )
130
- );
131
- }
132
-
121
+ $ this ->checkEntityAssociations ($ model );
133
122
if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
134
123
$ productRewrites = $ this ->productAppendRewrites ->getProductUrlRewrites (
135
124
$ this ->_getProduct (),
136
125
[$ this ->getRequest ()->getParam ('store_id ' , 0 )]
137
126
);
138
127
$ productRewrites = array_merge (...$ productRewrites );
139
- /** @var UrlRewrite $rewrite */
140
- foreach ($ productRewrites as $ rewrite ) {
141
- if ($ rewrite ->getRequestPath () != $ model ->getRequestPath ()) {
142
- $ missingRewrite = $ this ->_objectManager ->create (\Magento \UrlRewrite \Model \UrlRewrite::class);
143
- $ missingRewrite ->setEntityType (self ::ENTITY_TYPE_PRODUCT )
144
- ->setRequestPath ($ rewrite ->getRequestPath ())
145
- ->setTargetPath ($ rewrite ->getTargetPath ())
146
- ->setRedirectType ($ rewrite ->getRedirectType ())
147
- ->setStoreId ($ rewrite ->getStoreId ())
148
- ->setDescription ($ rewrite ->getDescription ())
149
- ->setMetadata ($ rewrite ->getMetadata ());
150
- $ this ->missingRewrites [] = $ missingRewrite ;
151
- if ($ rewrite ->getTargetPath () == $ targetPath ) {
152
- $ targetPath = $ rewrite ->getRequestPath ();
153
- }
154
- }
155
- }
128
+ $ targetPath = $ this ->getAdjustedTargetPath ($ model , $ productRewrites , $ targetPath );
129
+ $ this ->setMissingRewrites ($ model , $ productRewrites );
130
+ }
131
+ }
132
+ }
133
+
134
+ return $ targetPath ;
135
+ }
136
+
137
+ /**
138
+ * Checks for missing product rewrites
139
+ *
140
+ * @param \Magento\UrlRewrite\Model\UrlRewrite $model
141
+ * @param array $rewrites
142
+ * @return void
143
+ */
144
+ private function setMissingRewrites (\Magento \UrlRewrite \Model \UrlRewrite $ model , array $ rewrites ): void
145
+ {
146
+ if (!empty ($ rewrites )) {
147
+ foreach ($ rewrites as $ rewrite ) {
148
+ if ($ rewrite ->getRequestPath () != $ model ->getRequestPath ()) {
149
+ $ this ->missingRewrites [] = $ this ->generateRewriteModel ($ rewrite );
156
150
}
157
151
}
158
152
}
153
+ }
159
154
160
- $ model ->setTargetPath ($ targetPath );
155
+ /**
156
+ * Checks for potential target path adjustments
157
+ *
158
+ * @param \Magento\UrlRewrite\Model\UrlRewrite $model
159
+ * @param array $productRewrites
160
+ * @param string $canonicalTargetPath
161
+ * @return string|null
162
+ */
163
+ private function getAdjustedTargetPath (
164
+ \Magento \UrlRewrite \Model \UrlRewrite $ model ,
165
+ array $ productRewrites ,
166
+ string $ canonicalTargetPath
167
+ ): ?string {
168
+ if (empty ($ productRewrites )) {
169
+ return null ;
170
+ }
171
+
172
+ foreach ($ productRewrites as $ rewrite ) {
173
+ if ($ rewrite ->getRequestPath () != $ model ->getRequestPath ()) {
174
+ $ this ->missingRewrites [] = $ this ->generateRewriteModel ($ rewrite );
175
+ if ($ rewrite ->getTargetPath () == $ canonicalTargetPath ) {
176
+ return $ rewrite ->getRequestPath ();
177
+ }
178
+ }
179
+ }
180
+
181
+ return $ canonicalTargetPath ;
182
+ }
183
+
184
+ /**
185
+ * Generates rewrite model
186
+ *
187
+ * @param UrlRewrite $rewrite
188
+ * @return \Magento\UrlRewrite\Model\UrlRewrite
189
+ */
190
+ private function generateRewriteModel (UrlRewrite $ rewrite ): \Magento \UrlRewrite \Model \UrlRewrite
191
+ {
192
+ $ rewriteModel = $ this ->_objectManager ->create (\Magento \UrlRewrite \Model \UrlRewrite::class);
193
+ $ rewriteModel ->setEntityType (self ::ENTITY_TYPE_PRODUCT )
194
+ ->setRequestPath ($ rewrite ->getRequestPath ())
195
+ ->setTargetPath ($ rewrite ->getTargetPath ())
196
+ ->setRedirectType ($ rewrite ->getRedirectType ())
197
+ ->setStoreId ($ rewrite ->getStoreId ())
198
+ ->setDescription ($ rewrite ->getDescription ())
199
+ ->setMetadata ($ rewrite ->getMetadata ());
200
+
201
+ return $ rewriteModel ;
202
+ }
203
+
204
+ /**
205
+ * Checks if rewrite can be created for product or category
206
+ *
207
+ * @param \Magento\UrlRewrite\Model\UrlRewrite $model
208
+ * @return void
209
+ * @throws LocalizedException
210
+ */
211
+ private function checkEntityAssociations (\Magento \UrlRewrite \Model \UrlRewrite $ model ): void
212
+ {
213
+ $ check = $ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ?
214
+ $ this ->_getProduct ()->canBeShowInCategory ($ this ->_getCategory ()->getId ()) &&
215
+ in_array ($ model ->getStoreId (), $ this ->_getProduct ()->getStoreIds ()) :
216
+ $ this ->_getCategory ()->getStoreId () == $ model ->getStoreId ();
217
+ if (false === $ check ) {
218
+ throw new LocalizedException (
219
+ $ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT
220
+ ? __ ("The selected product isn't associated with the selected store or category. " )
221
+ : __ ("The selected category isn't associated with the selected store. " )
222
+ );
223
+ }
161
224
}
162
225
163
226
/**
0 commit comments