65 $storeId = $this->_objectManager->get(
67 )->getStore()->getId();
85 if (!$this->_formKeyValidator->validate($this->getRequest())) {
86 $this->messageManager->addErrorMessage(
87 __(
'Your session has expired')
89 return $this->resultRedirectFactory->create()->setPath(
'*/*/');
96 $filter = new \Zend_Filter_LocalizedToNormalized(
97 [
'locale' => $this->_objectManager->get(
98 \
Magento\Framework\Locale\ResolverInterface::class
105 $related = $this->
getRequest()->getParam(
'related_product');
115 if (!empty($related)) {
116 $this->cart->addProductsByIds(explode(
',', $related));
124 $this->_eventManager->dispatch(
125 'checkout_cart_add_product_complete',
129 if (!$this->_checkoutSession->getNoCartRedirect(
true)) {
130 if (!$this->cart->getQuote()->getHasError()) {
131 if ($this->shouldRedirectToCart()) {
133 'You added %1 to your shopping cart.',
136 $this->messageManager->addSuccessMessage(
$message);
138 $this->messageManager->addComplexSuccessMessage(
139 'addCartSuccessMessage',
141 'product_name' =>
$product->getName(),
142 'cart_url' => $this->getCartUrl(),
150 if ($this->_checkoutSession->getUseNotice(
true)) {
151 $this->messageManager->addNoticeMessage(
152 $this->_objectManager->get(\
Magento\Framework\Escaper::class)->escapeHtml($e->getMessage())
155 $messages = array_unique(explode(
"\n", $e->getMessage()));
157 $this->messageManager->addErrorMessage(
158 $this->_objectManager->get(\
Magento\Framework\Escaper::class)->escapeHtml(
$message)
163 $url = $this->_checkoutSession->getRedirectUrl(
true);
166 $url = $this->
_redirect->getRedirectUrl($this->getCartUrl());
171 $this->messageManager->addExceptionMessage(
173 __(
'We can\'t add this item to your shopping cart right now.')
175 $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
190 return parent::_goBack($backUrl);
195 if ($backUrl || $backUrl = $this->
getBackUrl()) {
200 'statusText' =>
__(
'Out of stock')
206 $this->_objectManager->get(\
Magento\Framework\
Json\Helper\Data::class)->jsonEncode(
$result)
215 private function getCartUrl()
217 return $this->_url->getUrl(
'checkout/cart', [
'_secure' =>
true]);
225 private function shouldRedirectToCart()
227 return $this->_scopeConfig->isSetFlag(
228 'checkout/cart/redirect_to_cart',
goBack($backUrl=null, $product=null)
getBackUrl($defaultUrl=null)
_redirect($path, $arguments=[])
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, CustomerCart $cart, ProductRepositoryInterface $productRepository)