9
9
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
11
use Magento \Checkout \Model \Cart as CustomerCart ;
12
+ use Magento \Framework \App \ResponseInterface ;
13
+ use Magento \Framework \Controller \ResultInterface ;
12
14
use Magento \Framework \Exception \NoSuchEntityException ;
13
15
14
16
/**
@@ -77,7 +79,7 @@ protected function _initProduct()
77
79
/**
78
80
* Add product to shopping cart action
79
81
*
80
- * @return \Magento\Framework\Controller\Result\Redirect
82
+ * @return ResponseInterface|ResultInterface
81
83
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
82
84
*/
83
85
public function execute ()
@@ -90,7 +92,6 @@ public function execute()
90
92
}
91
93
92
94
$ params = $ this ->getRequest ()->getParams ();
93
-
94
95
try {
95
96
if (isset ($ params ['qty ' ])) {
96
97
$ filter = new \Zend_Filter_LocalizedToNormalized (
@@ -104,9 +105,7 @@ public function execute()
104
105
$ product = $ this ->_initProduct ();
105
106
$ related = $ this ->getRequest ()->getParam ('related_product ' );
106
107
107
- /**
108
- * Check product availability
109
- */
108
+ /** Check product availability */
110
109
if (!$ product ) {
111
110
return $ this ->goBack ();
112
111
}
@@ -115,7 +114,6 @@ public function execute()
115
114
if (!empty ($ related )) {
116
115
$ this ->cart ->addProductsByIds (explode (', ' , $ related ));
117
116
}
118
-
119
117
$ this ->cart ->save ();
120
118
121
119
/**
@@ -127,21 +125,25 @@ public function execute()
127
125
);
128
126
129
127
if (!$ this ->_checkoutSession ->getNoCartRedirect (true )) {
130
- if (!$ this ->cart ->getQuote ()->getHasError ()) {
131
- if ($ this ->shouldRedirectToCart ()) {
132
- $ message = __ (
133
- 'You added %1 to your shopping cart. ' ,
134
- $ product ->getName ()
135
- );
136
- $ this ->messageManager ->addSuccessMessage ($ message );
137
- } else {
138
- $ this ->messageManager ->addComplexSuccessMessage (
139
- 'addCartSuccessMessage ' ,
140
- [
141
- 'product_name ' => $ product ->getName (),
142
- 'cart_url ' => $ this ->getCartUrl (),
143
- ]
144
- );
128
+ if ($ this ->shouldRedirectToCart ()) {
129
+ $ message = __ (
130
+ 'You added %1 to your shopping cart. ' ,
131
+ $ product ->getName ()
132
+ );
133
+ $ this ->messageManager ->addSuccessMessage ($ message );
134
+ } else {
135
+ $ this ->messageManager ->addComplexSuccessMessage (
136
+ 'addCartSuccessMessage ' ,
137
+ [
138
+ 'product_name ' => $ product ->getName (),
139
+ 'cart_url ' => $ this ->getCartUrl (),
140
+ ]
141
+ );
142
+ }
143
+ if ($ this ->cart ->getQuote ()->getHasError ()) {
144
+ $ errors = $ this ->cart ->getQuote ()->getErrors ();
145
+ foreach ($ errors as $ error ) {
146
+ $ this ->messageManager ->addErrorMessage ($ error ->getText ());
145
147
}
146
148
}
147
149
return $ this ->goBack (null , $ product );
@@ -161,7 +163,6 @@ public function execute()
161
163
}
162
164
163
165
$ url = $ this ->_checkoutSession ->getRedirectUrl (true );
164
-
165
166
if (!$ url ) {
166
167
$ url = $ this ->_redirect ->getRedirectUrl ($ this ->getCartUrl ());
167
168
}
@@ -175,14 +176,16 @@ public function execute()
175
176
$ this ->_objectManager ->get (\Psr \Log \LoggerInterface::class)->critical ($ e );
176
177
return $ this ->goBack ();
177
178
}
179
+
180
+ return $ this ->getResponse ();
178
181
}
179
182
180
183
/**
181
184
* Resolve response
182
185
*
183
186
* @param string $backUrl
184
187
* @param \Magento\Catalog\Model\Product $product
185
- * @return $this|\Magento\Framework\Controller\Result\Redirect
188
+ * @return ResponseInterface|ResultInterface
186
189
*/
187
190
protected function goBack ($ backUrl = null , $ product = null )
188
191
{
@@ -205,6 +208,8 @@ protected function goBack($backUrl = null, $product = null)
205
208
$ this ->getResponse ()->representJson (
206
209
$ this ->_objectManager ->get (\Magento \Framework \Json \Helper \Data::class)->jsonEncode ($ result )
207
210
);
211
+
212
+ return $ this ->getResponse ();
208
213
}
209
214
210
215
/**
0 commit comments