@@ -47,6 +47,11 @@ class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implemen
47
47
*/
48
48
protected AppendUrlRewritesToProducts $ productAppendRewrites ;
49
49
50
+ /**
51
+ * @var array
52
+ */
53
+ protected array $ missingRewrites = [];
54
+
50
55
/**
51
56
* @param Context $context
52
57
* @param ProductUrlPathGenerator $productUrlPathGenerator
@@ -61,7 +66,7 @@ public function __construct(
61
66
\Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
62
67
\Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator $ cmsPageUrlPathGenerator ,
63
68
UrlFinderInterface $ urlFinder ,
64
- AppendUrlRewritesToProducts $ productAppendRewrites = null
69
+ ? AppendUrlRewritesToProducts $ productAppendRewrites = null
65
70
) {
66
71
parent ::__construct ($ context );
67
72
$ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -91,58 +96,57 @@ protected function _handleCatalogUrlRewrite($model)
91
96
$ model ->setMetadata (['category_id ' => $ categoryId ]);
92
97
}
93
98
}
94
- $ model -> setTargetPath ( $ this ->generateTargetPath ($ model) );
99
+ $ this ->generateTargetPath ($ model );
95
100
}
96
101
}
97
102
98
103
/**
99
104
* Generate Target Path
100
105
*
101
106
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
102
- * @return string
103
107
* @throws \Magento\Framework\Exception\LocalizedException
104
108
*/
105
109
protected function generateTargetPath ($ model )
106
110
{
107
111
$ targetPath = $ this ->getCanonicalTargetPath ();
108
112
if ($ model ->getRedirectType () && !$ model ->getIsAutogenerated ()) {
109
- $ data = [
113
+ if ( $ rewrite = $ this -> urlFinder -> findOneByData ( [
110
114
UrlRewrite::ENTITY_ID => $ model ->getEntityId (),
111
115
UrlRewrite::TARGET_PATH => $ targetPath ,
112
116
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 {
117
121
if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
118
- $ this ->productAppendRewrites ->execute (
119
- [ $ this ->_getProduct ()] ,
122
+ $ productRewrites = $ this ->productAppendRewrites ->getProductUrlRewrites (
123
+ $ this ->_getProduct (),
120
124
[$ this ->getRequest ()->getParam ('store_id ' , 0 )]
121
125
);
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 ) {
124
136
throw new LocalizedException (
125
137
__ (
126
138
"The selected product isn't associated with the selected store or category. "
127
139
)
128
140
);
129
141
}
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
- }
137
142
} else {
138
143
throw new
139
144
LocalizedException (__ ("The selected category isn't associated with the selected store. " ));
140
145
}
141
- } else {
142
- $ targetPath = $ rewrite ->getRequestPath ();
143
146
}
144
147
}
145
- return $ targetPath ;
148
+
149
+ $ model ->setTargetPath ($ targetPath );
146
150
}
147
151
148
152
/**
@@ -207,10 +211,12 @@ public function execute()
207
211
->setStoreId ($ this ->getRequest ()->getParam ('store_id ' , 0 ))
208
212
->setDescription ($ this ->getRequest ()->getParam ('description ' ));
209
213
210
- $ this ->_handleCatalogUrlRewrite ($ model );
211
214
$ this ->_handleCmsPageUrlRewrite ($ model );
215
+ $ this ->_handleCatalogUrlRewrite ($ model );
212
216
$ model ->save ();
213
-
217
+ if (!empty ($ this ->missingRewrites )) {
218
+ $ this ->productAppendRewrites ->saveProductUrlRewrites ($ this ->missingRewrites );
219
+ }
214
220
$ this ->messageManager ->addSuccess (__ ('The URL Rewrite has been saved. ' ));
215
221
$ this ->_redirect ('adminhtml/*/ ' );
216
222
return ;
0 commit comments