Skip to content

Commit c8196f3

Browse files
ENGCOM-5790: fix issue #24366 - Updating cart quantity #24380
- Merge Pull Request #24380 from GDauer/magento2:bugfix - Merged commits: 1. 48ae1fc 2. cc78567 3. 0a67f76 4. d2a09d3 5. 720799b 6. 57a3546 7. 650c215
2 parents 90b9c43 + 650c215 commit c8196f3

File tree

2 files changed

+72
-18
lines changed

2 files changed

+72
-18
lines changed

app/code/Magento/Checkout/Controller/Cart/UpdateItemQty.php

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@
88
namespace Magento\Checkout\Controller\Cart;
99

1010
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
11+
use Magento\Checkout\Model\Session as CheckoutSession;
12+
use Magento\Framework\App\Action\Action;
1113
use Magento\Framework\App\Action\Context;
14+
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
1216
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Checkout\Model\Session as CheckoutSession;
17+
use Magento\Framework\Exception\NotFoundException;
1418
use Magento\Framework\Serialize\Serializer\Json;
15-
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
1619
use Magento\Quote\Model\Quote\Item;
1720
use Psr\Log\LoggerInterface;
1821

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
2028
{
29+
2130
/**
2231
* @var RequestQuantityProcessor
2332
*/
@@ -44,13 +53,16 @@ class UpdateItemQty extends \Magento\Framework\App\Action\Action
4453
private $logger;
4554

4655
/**
47-
* @param Context $context,
56+
* UpdateItemQty constructor
57+
*
58+
* @param Context $context
4859
* @param RequestQuantityProcessor $quantityProcessor
4960
* @param FormKeyValidator $formKeyValidator
5061
* @param CheckoutSession $checkoutSession
5162
* @param Json $json
5263
* @param LoggerInterface $logger
5364
*/
65+
5466
public function __construct(
5567
Context $context,
5668
RequestQuantityProcessor $quantityProcessor,
@@ -68,30 +80,26 @@ public function __construct(
6880
}
6981

7082
/**
83+
* Controller execute method
84+
*
7185
* @return void
7286
*/
7387
public function execute()
7488
{
7589
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();
8192

8293
$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);
8896

8997
$cartData = $this->quantityProcessor->process($cartData);
9098
$quote = $this->checkoutSession->getQuote();
9199

92100
foreach ($cartData as $itemId => $itemInfo) {
93101
$item = $quote->getItemById($itemId);
94-
$qty = isset($itemInfo['qty']) ? (double)$itemInfo['qty'] : 0;
102+
$qty = isset($itemInfo['qty']) ? (double) $itemInfo['qty'] : 0;
95103
if ($item) {
96104
$this->updateItemQuantity($item, $qty);
97105
}
@@ -111,11 +119,13 @@ public function execute()
111119
*
112120
* @param Item $item
113121
* @param float $qty
122+
* @return void
114123
* @throws LocalizedException
115124
*/
116125
private function updateItemQuantity(Item $item, float $qty)
117126
{
118127
if ($qty > 0) {
128+
$item->clearMessage();
119129
$item->setQty($qty);
120130

121131
if ($item->getHasError()) {
@@ -145,9 +155,7 @@ private function jsonResponse(string $error = '')
145155
*/
146156
private function getResponseData(string $error = ''): array
147157
{
148-
$response = [
149-
'success' => true,
150-
];
158+
$response = ['success' => true];
151159

152160
if (!empty($error)) {
153161
$response = [
@@ -158,4 +166,48 @@ private function getResponseData(string $error = ''): array
158166

159167
return $response;
160168
}
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+
}
161213
}

dev/tests/integration/testsuite/Magento/Checkout/Controller/Cart/UpdateItemQtyTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Model\Product;
1111
use Magento\Checkout\Model\Session;
1212
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\App\Request\Http as HttpRequest;
1314
use Magento\Framework\Data\Form\FormKey;
1415
use Magento\Framework\Serialize\Serializer\Json;
1516

@@ -81,6 +82,7 @@ public function testExecute($requestQuantity, $expectedResponse)
8182
];
8283
}
8384

85+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
8486
$this->getRequest()->setPostValue($request);
8587
$this->dispatch('checkout/cart/updateItemQty');
8688
$response = $this->getResponse()->getBody();

0 commit comments

Comments
 (0)