@@ -118,6 +118,18 @@ 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
133
if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
122
134
$ productRewrites = $ this ->productAppendRewrites ->getProductUrlRewrites (
123
135
$ this ->_getProduct (),
@@ -126,22 +138,21 @@ protected function generateTargetPath($model)
126
138
$ productRewrites = array_merge (...$ productRewrites );
127
139
/** @var UrlRewrite $rewrite */
128
140
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
+ }
133
154
}
134
155
}
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. " ));
145
156
}
146
157
}
147
158
}
@@ -215,8 +226,11 @@ public function execute()
215
226
$ this ->_handleCatalogUrlRewrite ($ model );
216
227
$ model ->save ();
217
228
if (!empty ($ this ->missingRewrites )) {
218
- $ this ->productAppendRewrites ->saveProductUrlRewrites ($ this ->missingRewrites );
229
+ foreach ($ this ->missingRewrites as $ missingRewrite ) {
230
+ $ missingRewrite ->save ();
231
+ }
219
232
}
233
+
220
234
$ this ->messageManager ->addSuccess (__ ('The URL Rewrite has been saved. ' ));
221
235
$ this ->_redirect ('adminhtml/*/ ' );
222
236
return ;
0 commit comments