@@ -118,6 +118,18 @@ 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+
121133 if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
122134 $ productRewrites = $ this ->productAppendRewrites ->getProductUrlRewrites (
123135 $ this ->_getProduct (),
@@ -126,22 +138,21 @@ protected function generateTargetPath($model)
126138 $ productRewrites = array_merge (...$ productRewrites );
127139 /** @var UrlRewrite $rewrite */
128140 foreach ($ productRewrites as $ rewrite ) {
129- if ($ rewrite ->getTargetPath () == $ model ->getTargetPath ()) {
130- $ targetPath = $ rewrite ->getRequestPath ();
131- } else {
132- $ this ->missingRewrites [] = $ 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+ }
133154 }
134155 }
135- if (!$ targetPath ) {
136- throw new LocalizedException (
137- __ (
138- "The selected product isn't associated with the selected store or category. "
139- )
140- );
141- }
142- } else {
143- throw new
144- LocalizedException (__ ("The selected category isn't associated with the selected store. " ));
145156 }
146157 }
147158 }
@@ -215,8 +226,11 @@ public function execute()
215226 $ this ->_handleCatalogUrlRewrite ($ model );
216227 $ model ->save ();
217228 if (!empty ($ this ->missingRewrites )) {
218- $ this ->productAppendRewrites ->saveProductUrlRewrites ($ this ->missingRewrites );
229+ foreach ($ this ->missingRewrites as $ missingRewrite ) {
230+ $ missingRewrite ->save ();
231+ }
219232 }
233+
220234 $ this ->messageManager ->addSuccess (__ ('The URL Rewrite has been saved. ' ));
221235 $ this ->_redirect ('adminhtml/*/ ' );
222236 return ;
0 commit comments