6 declare(strict_types=1);
17 use Psr\Log\LoggerInterface;
24 private $quantityProcessor;
29 private $formKeyValidator;
34 private $checkoutSession;
57 FormKeyValidator $formKeyValidator,
62 $this->quantityProcessor = $quantityProcessor;
63 $this->formKeyValidator = $formKeyValidator;
64 $this->checkoutSession = $checkoutSession;
67 parent::__construct($context);
76 if (!$this->formKeyValidator->validate($this->getRequest())) {
78 __(
'Something went wrong while saving the page. Please refresh the page and try again.')
82 $cartData = $this->
getRequest()->getParam(
'cart');
83 if (!is_array($cartData)) {
85 __(
'Something went wrong while saving the page. Please refresh the page and try again.')
89 $cartData = $this->quantityProcessor->process($cartData);
90 $quote = $this->checkoutSession->getQuote();
92 foreach ($cartData as $itemId => $itemInfo) {
94 $qty = isset($itemInfo[
'qty']) ? (double)$itemInfo[
'qty'] : 0;
96 $this->updateItemQuantity(
$item, $qty);
100 $this->jsonResponse();
102 $this->jsonResponse($e->getMessage());
104 $this->logger->critical($e->getMessage());
105 $this->jsonResponse(
'Something went wrong while saving the page. Please refresh the page and try again.');
116 private function updateItemQuantity(
Item $item,
float $qty)
121 if (
$item->getHasError()) {
133 private function jsonResponse(
string $error =
'')
136 $this->json->serialize($this->getResponseData($error))
146 private function getResponseData(
string $error =
''): array
152 if (!empty($error)) {
155 'error_message' => $error,
__construct(Context $context, RequestQuantityProcessor $quantityProcessor, FormKeyValidator $formKeyValidator, CheckoutSession $checkoutSession, Json $json, LoggerInterface $logger)