Skip to content

Commit 619d155

Browse files
committed
Merge remote-tracking branch 'origin/MC-33899' into 2.4-develop-pr27
2 parents 6b3b90b + fd3cf34 commit 619d155

File tree

1 file changed

+28
-23
lines changed
  • app/code/Magento/Checkout/Controller/Cart

1 file changed

+28
-23
lines changed

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Checkout\Model\Cart as CustomerCart;
12+
use Magento\Framework\App\ResponseInterface;
13+
use Magento\Framework\Controller\ResultInterface;
1214
use Magento\Framework\Exception\NoSuchEntityException;
1315

1416
/**
@@ -77,7 +79,7 @@ protected function _initProduct()
7779
/**
7880
* Add product to shopping cart action
7981
*
80-
* @return \Magento\Framework\Controller\Result\Redirect
82+
* @return ResponseInterface|ResultInterface
8183
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8284
*/
8385
public function execute()
@@ -90,7 +92,6 @@ public function execute()
9092
}
9193

9294
$params = $this->getRequest()->getParams();
93-
9495
try {
9596
if (isset($params['qty'])) {
9697
$filter = new \Zend_Filter_LocalizedToNormalized(
@@ -104,9 +105,7 @@ public function execute()
104105
$product = $this->_initProduct();
105106
$related = $this->getRequest()->getParam('related_product');
106107

107-
/**
108-
* Check product availability
109-
*/
108+
/** Check product availability */
110109
if (!$product) {
111110
return $this->goBack();
112111
}
@@ -115,7 +114,6 @@ public function execute()
115114
if (!empty($related)) {
116115
$this->cart->addProductsByIds(explode(',', $related));
117116
}
118-
119117
$this->cart->save();
120118

121119
/**
@@ -127,21 +125,25 @@ public function execute()
127125
);
128126

129127
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());
145147
}
146148
}
147149
return $this->goBack(null, $product);
@@ -161,7 +163,6 @@ public function execute()
161163
}
162164

163165
$url = $this->_checkoutSession->getRedirectUrl(true);
164-
165166
if (!$url) {
166167
$url = $this->_redirect->getRedirectUrl($this->getCartUrl());
167168
}
@@ -175,14 +176,16 @@ public function execute()
175176
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
176177
return $this->goBack();
177178
}
179+
180+
return $this->getResponse();
178181
}
179182

180183
/**
181184
* Resolve response
182185
*
183186
* @param string $backUrl
184187
* @param \Magento\Catalog\Model\Product $product
185-
* @return $this|\Magento\Framework\Controller\Result\Redirect
188+
* @return ResponseInterface|ResultInterface
186189
*/
187190
protected function goBack($backUrl = null, $product = null)
188191
{
@@ -205,6 +208,8 @@ protected function goBack($backUrl = null, $product = null)
205208
$this->getResponse()->representJson(
206209
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
207210
);
211+
212+
return $this->getResponse();
208213
}
209214

210215
/**

0 commit comments

Comments
 (0)