Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateItemQty.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
17 use Psr\Log\LoggerInterface;
18 
20 {
24  private $quantityProcessor;
25 
29  private $formKeyValidator;
30 
34  private $checkoutSession;
35 
39  private $json;
40 
44  private $logger;
45 
54  public function __construct(
55  Context $context,
56  RequestQuantityProcessor $quantityProcessor,
57  FormKeyValidator $formKeyValidator,
58  CheckoutSession $checkoutSession,
59  Json $json,
60  LoggerInterface $logger
61  ) {
62  $this->quantityProcessor = $quantityProcessor;
63  $this->formKeyValidator = $formKeyValidator;
64  $this->checkoutSession = $checkoutSession;
65  $this->json = $json;
66  $this->logger = $logger;
67  parent::__construct($context);
68  }
69 
73  public function execute()
74  {
75  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  }
81 
82  $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  }
88 
89  $cartData = $this->quantityProcessor->process($cartData);
90  $quote = $this->checkoutSession->getQuote();
91 
92  foreach ($cartData as $itemId => $itemInfo) {
93  $item = $quote->getItemById($itemId);
94  $qty = isset($itemInfo['qty']) ? (double)$itemInfo['qty'] : 0;
95  if ($item) {
96  $this->updateItemQuantity($item, $qty);
97  }
98  }
99 
100  $this->jsonResponse();
101  } catch (LocalizedException $e) {
102  $this->jsonResponse($e->getMessage());
103  } catch (\Exception $e) {
104  $this->logger->critical($e->getMessage());
105  $this->jsonResponse('Something went wrong while saving the page. Please refresh the page and try again.');
106  }
107  }
108 
116  private function updateItemQuantity(Item $item, float $qty)
117  {
118  if ($qty > 0) {
119  $item->setQty($qty);
120 
121  if ($item->getHasError()) {
122  throw new LocalizedException(__($item->getMessage()));
123  }
124  }
125  }
126 
133  private function jsonResponse(string $error = '')
134  {
135  $this->getResponse()->representJson(
136  $this->json->serialize($this->getResponseData($error))
137  );
138  }
139 
146  private function getResponseData(string $error = ''): array
147  {
148  $response = [
149  'success' => true,
150  ];
151 
152  if (!empty($error)) {
153  $response = [
154  'success' => false,
155  'error_message' => $error,
156  ];
157  }
158 
159  return $response;
160  }
161 }
$response
Definition: 404.php:11
__construct(Context $context, RequestQuantityProcessor $quantityProcessor, FormKeyValidator $formKeyValidator, CheckoutSession $checkoutSession, Json $json, LoggerInterface $logger)
$quote
__()
Definition: __.php:13
$logger