@@ -47,6 +47,11 @@ class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implemen
4747 */
4848 protected AppendUrlRewritesToProducts $ productAppendRewrites ;
4949
50+ /**
51+ * @var array
52+ */
53+ protected array $ missingRewrites = [];
54+
5055 /**
5156 * @param Context $context
5257 * @param ProductUrlPathGenerator $productUrlPathGenerator
@@ -61,7 +66,7 @@ public function __construct(
6166 \Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
6267 \Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator $ cmsPageUrlPathGenerator ,
6368 UrlFinderInterface $ urlFinder ,
64- AppendUrlRewritesToProducts $ productAppendRewrites = null
69+ ? AppendUrlRewritesToProducts $ productAppendRewrites = null
6570 ) {
6671 parent ::__construct ($ context );
6772 $ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -91,58 +96,57 @@ protected function _handleCatalogUrlRewrite($model)
9196 $ model ->setMetadata (['category_id ' => $ categoryId ]);
9297 }
9398 }
94- $ model -> setTargetPath ( $ this ->generateTargetPath ($ model) );
99+ $ this ->generateTargetPath ($ model );
95100 }
96101 }
97102
98103 /**
99104 * Generate Target Path
100105 *
101106 * @param \Magento\UrlRewrite\Model\UrlRewrite $model
102- * @return string
103107 * @throws \Magento\Framework\Exception\LocalizedException
104108 */
105109 protected function generateTargetPath ($ model )
106110 {
107111 $ targetPath = $ this ->getCanonicalTargetPath ();
108112 if ($ model ->getRedirectType () && !$ model ->getIsAutogenerated ()) {
109- $ data = [
113+ if ( $ rewrite = $ this -> urlFinder -> findOneByData ( [
110114 UrlRewrite::ENTITY_ID => $ model ->getEntityId (),
111115 UrlRewrite::TARGET_PATH => $ targetPath ,
112116 UrlRewrite::ENTITY_TYPE => $ model ->getEntityType (),
113- UrlRewrite::STORE_ID => $ model ->getStoreId (),
114- ];
115- $ rewrite = $ this -> urlFinder -> findOneByData ( $ data );
116- if (! $ rewrite ) {
117+ UrlRewrite::STORE_ID => $ model ->getStoreId ()
118+ ])) {
119+ $ targetPath = $ rewrite -> getRequestPath ( );
120+ } else {
117121 if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
118- $ this ->productAppendRewrites ->execute (
119- [ $ this ->_getProduct ()] ,
122+ $ productRewrites = $ this ->productAppendRewrites ->getProductUrlRewrites (
123+ $ this ->_getProduct (),
120124 [$ this ->getRequest ()->getParam ('store_id ' , 0 )]
121125 );
122- $ rewrite = $ this ->urlFinder ->findOneByData ($ data );
123- if (!$ rewrite ) {
126+ $ productRewrites = array_merge (...$ productRewrites );
127+ /** @var UrlRewrite $rewrite */
128+ foreach ($ productRewrites as $ rewrite ) {
129+ if ($ rewrite ->getTargetPath () == $ model ->getTargetPath ()) {
130+ $ targetPath = $ rewrite ->getRequestPath ();
131+ } else {
132+ $ this ->missingRewrites [] = $ rewrite ;
133+ }
134+ }
135+ if (!$ targetPath ) {
124136 throw new LocalizedException (
125137 __ (
126138 "The selected product isn't associated with the selected store or category. "
127139 )
128140 );
129141 }
130- $ targetPath = $ rewrite ->getRequestPath ();
131- if ($ rewrite ->getRequestPath () == $ model ->getRequestPath () &&
132- $ rewrite ->getStoreId () == $ model ->getStoreId ()) {
133- $ obsoleteRewrite = $ this ->_objectManager ->create (\Magento \UrlRewrite \Model \UrlRewrite::class);
134- $ obsoleteRewrite ->load ($ rewrite ->getUrlRewriteId ());
135- $ obsoleteRewrite ->delete ();
136- }
137142 } else {
138143 throw new
139144 LocalizedException (__ ("The selected category isn't associated with the selected store. " ));
140145 }
141- } else {
142- $ targetPath = $ rewrite ->getRequestPath ();
143146 }
144147 }
145- return $ targetPath ;
148+
149+ $ model ->setTargetPath ($ targetPath );
146150 }
147151
148152 /**
@@ -207,10 +211,12 @@ public function execute()
207211 ->setStoreId ($ this ->getRequest ()->getParam ('store_id ' , 0 ))
208212 ->setDescription ($ this ->getRequest ()->getParam ('description ' ));
209213
210- $ this ->_handleCatalogUrlRewrite ($ model );
211214 $ this ->_handleCmsPageUrlRewrite ($ model );
215+ $ this ->_handleCatalogUrlRewrite ($ model );
212216 $ model ->save ();
213-
217+ if (!empty ($ this ->missingRewrites )) {
218+ $ this ->productAppendRewrites ->saveProductUrlRewrites ($ this ->missingRewrites );
219+ }
214220 $ this ->messageManager ->addSuccess (__ ('The URL Rewrite has been saved. ' ));
215221 $ this ->_redirect ('adminhtml/*/ ' );
216222 return ;
0 commit comments