8
8
namespace Magento \Checkout \Controller \Cart ;
9
9
10
10
use Magento \Checkout \Model \Cart \RequestQuantityProcessor ;
11
+ use Magento \Checkout \Model \Session as CheckoutSession ;
12
+ use Magento \Framework \App \Action \Action ;
11
13
use Magento \Framework \App \Action \Context ;
14
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
15
+ use Magento \Framework \Data \Form \FormKey \Validator as FormKeyValidator ;
12
16
use Magento \Framework \Exception \LocalizedException ;
13
- use Magento \Checkout \ Model \ Session as CheckoutSession ;
17
+ use Magento \Framework \ Exception \ NotFoundException ;
14
18
use Magento \Framework \Serialize \Serializer \Json ;
15
- use Magento \Framework \Data \Form \FormKey \Validator as FormKeyValidator ;
16
19
use Magento \Quote \Model \Quote \Item ;
17
20
use Psr \Log \LoggerInterface ;
18
21
19
- class UpdateItemQty extends \Magento \Framework \App \Action \Action
22
+ /**
23
+ * UpdateItemQty ajax request
24
+ *
25
+ * @package Magento\Checkout\Controller\Cart
26
+ */
27
+ class UpdateItemQty extends Action implements HttpPostActionInterface
20
28
{
29
+
21
30
/**
22
31
* @var RequestQuantityProcessor
23
32
*/
@@ -44,13 +53,16 @@ class UpdateItemQty extends \Magento\Framework\App\Action\Action
44
53
private $ logger ;
45
54
46
55
/**
47
- * @param Context $context,
56
+ * UpdateItemQty constructor
57
+ *
58
+ * @param Context $context
48
59
* @param RequestQuantityProcessor $quantityProcessor
49
60
* @param FormKeyValidator $formKeyValidator
50
61
* @param CheckoutSession $checkoutSession
51
62
* @param Json $json
52
63
* @param LoggerInterface $logger
53
64
*/
65
+
54
66
public function __construct (
55
67
Context $ context ,
56
68
RequestQuantityProcessor $ quantityProcessor ,
@@ -68,30 +80,26 @@ public function __construct(
68
80
}
69
81
70
82
/**
83
+ * Controller execute method
84
+ *
71
85
* @return void
72
86
*/
73
87
public function execute ()
74
88
{
75
89
try {
76
- if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
77
- throw new LocalizedException (
78
- __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
79
- );
80
- }
90
+ $ this ->validateRequest ();
91
+ $ this ->validateFormKey ();
81
92
82
93
$ cartData = $ this ->getRequest ()->getParam ('cart ' );
83
- if (!is_array ($ cartData )) {
84
- throw new LocalizedException (
85
- __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
86
- );
87
- }
94
+
95
+ $ this ->validateCartData ($ cartData );
88
96
89
97
$ cartData = $ this ->quantityProcessor ->process ($ cartData );
90
98
$ quote = $ this ->checkoutSession ->getQuote ();
91
99
92
100
foreach ($ cartData as $ itemId => $ itemInfo ) {
93
101
$ item = $ quote ->getItemById ($ itemId );
94
- $ qty = isset ($ itemInfo ['qty ' ]) ? (double )$ itemInfo ['qty ' ] : 0 ;
102
+ $ qty = isset ($ itemInfo ['qty ' ]) ? (double ) $ itemInfo ['qty ' ] : 0 ;
95
103
if ($ item ) {
96
104
$ this ->updateItemQuantity ($ item , $ qty );
97
105
}
@@ -111,11 +119,13 @@ public function execute()
111
119
*
112
120
* @param Item $item
113
121
* @param float $qty
122
+ * @return void
114
123
* @throws LocalizedException
115
124
*/
116
125
private function updateItemQuantity (Item $ item , float $ qty )
117
126
{
118
127
if ($ qty > 0 ) {
128
+ $ item ->clearMessage ();
119
129
$ item ->setQty ($ qty );
120
130
121
131
if ($ item ->getHasError ()) {
@@ -145,9 +155,7 @@ private function jsonResponse(string $error = '')
145
155
*/
146
156
private function getResponseData (string $ error = '' ): array
147
157
{
148
- $ response = [
149
- 'success ' => true ,
150
- ];
158
+ $ response = ['success ' => true ];
151
159
152
160
if (!empty ($ error )) {
153
161
$ response = [
@@ -158,4 +166,48 @@ private function getResponseData(string $error = ''): array
158
166
159
167
return $ response ;
160
168
}
169
+
170
+ /**
171
+ * Validates the Request HTTP method
172
+ *
173
+ * @return void
174
+ * @throws NotFoundException
175
+ */
176
+ private function validateRequest ()
177
+ {
178
+ if ($ this ->getRequest ()->isPost () === false ) {
179
+ throw new NotFoundException (__ ('Page Not Found ' ));
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Validates form key
185
+ *
186
+ * @return void
187
+ * @throws LocalizedException
188
+ */
189
+ private function validateFormKey ()
190
+ {
191
+ if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
192
+ throw new LocalizedException (
193
+ __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
194
+ );
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Validates cart data
200
+ *
201
+ * @param array|null $cartData
202
+ * @return void
203
+ * @throws LocalizedException
204
+ */
205
+ private function validateCartData ($ cartData = null )
206
+ {
207
+ if (!is_array ($ cartData )) {
208
+ throw new LocalizedException (
209
+ __ ('Something went wrong while saving the page. Please refresh the page and try again. ' )
210
+ );
211
+ }
212
+ }
161
213
}
0 commit comments