@@ -66,7 +66,7 @@ public function __construct(
6666 \Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
6767 \Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator $ cmsPageUrlPathGenerator ,
6868 UrlFinderInterface $ urlFinder ,
69- ?AppendUrlRewritesToProducts $ productAppendRewrites = null
69+ ?AppendUrlRewritesToProducts $ productAppendRewrites = null
7070 ) {
7171 parent ::__construct ($ context );
7272 $ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -82,7 +82,7 @@ public function __construct(
8282 *
8383 * @param \Magento\UrlRewrite\Model\UrlRewrite $model
8484 * @return void
85- * @throws \Magento\Framework\Exception\ LocalizedException
85+ * @throws LocalizedException
8686 */
8787 protected function _handleCatalogUrlRewrite ($ model )
8888 {
@@ -96,17 +96,17 @@ protected function _handleCatalogUrlRewrite($model)
9696 $ model ->setMetadata (['category_id ' => $ categoryId ]);
9797 }
9898 }
99- $ this ->generateTargetPath ($ model );
99+ $ model -> setTargetPath ( $ this ->generateTargetPath ($ model) );
100100 }
101101 }
102102
103103 /**
104104 * Generate Target Path
105105 *
106106 * @param \Magento\UrlRewrite\Model\UrlRewrite $model
107- * @throws \Magento\Framework\Exception\ LocalizedException
107+ * @throws LocalizedException
108108 */
109- protected function generateTargetPath ($ model )
109+ protected function generateTargetPath ($ model ): ? string
110110 {
111111 $ targetPath = $ this ->getCanonicalTargetPath ();
112112 if ($ model ->getRedirectType () && !$ model ->getIsAutogenerated ()) {
@@ -118,46 +118,109 @@ protected function generateTargetPath($model)
118118 ])) {
119119 $ targetPath = $ rewrite ->getRequestPath ();
120120 } 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 );
133122 if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
134123 $ productRewrites = $ this ->productAppendRewrites ->getProductUrlRewrites (
135124 $ this ->_getProduct (),
136125 [$ this ->getRequest ()->getParam ('store_id ' , 0 )]
137126 );
138127 $ 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 );
156150 }
157151 }
158152 }
153+ }
159154
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+ }
161224 }
162225
163226 /**
0 commit comments