Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cart.php
Go to the documentation of this file.
1 <?php
7 
10 
14 abstract class Cart extends \Magento\Framework\App\Action\Action implements ViewInterface
15 {
19  protected $_scopeConfig;
20 
24  protected $_checkoutSession;
25 
29  protected $_storeManager;
30 
34  protected $_formKeyValidator;
35 
39  protected $cart;
40 
50  public function __construct(
51  \Magento\Framework\App\Action\Context $context,
52  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
53  \Magento\Checkout\Model\Session $checkoutSession,
55  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
57  ) {
58  $this->_formKeyValidator = $formKeyValidator;
59  $this->_scopeConfig = $scopeConfig;
60  $this->_checkoutSession = $checkoutSession;
61  $this->_storeManager = $storeManager;
62  $this->cart = $cart;
63  parent::__construct($context);
64  }
65 
73  protected function _goBack($backUrl = null)
74  {
75  $resultRedirect = $this->resultRedirectFactory->create();
76 
77  if ($backUrl || $backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) {
78  $resultRedirect->setUrl($backUrl);
79  }
80 
81  return $resultRedirect;
82  }
83 
90  protected function _isInternalUrl($url)
91  {
92  if (strpos($url, 'http') === false) {
93  return false;
94  }
95 
100  $store = $this->_storeManager->getStore();
101  $unsecure = strpos($url, $store->getBaseUrl()) === 0;
102  $secure = strpos($url, $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, true)) === 0;
103  return $unsecure || $secure;
104  }
105 
112  protected function getBackUrl($defaultUrl = null)
113  {
114  $returnUrl = $this->getRequest()->getParam('return_url');
115  if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
116  $this->messageManager->getMessages()->clear();
117  return $returnUrl;
118  }
119 
120  if ($this->shouldRedirectToCart() || $this->getRequest()->getParam('in_cart')) {
121  if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
122  $this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
123  }
124  return $this->_url->getUrl('checkout/cart');
125  }
126 
127  return $defaultUrl;
128  }
129 
135  private function shouldRedirectToCart()
136  {
137  return $this->_scopeConfig->isSetFlag(
138  'checkout/cart/redirect_to_cart',
140  );
141  }
142 }
getBackUrl($defaultUrl=null)
Definition: Cart.php:112
_redirect($path, $arguments=[])
Definition: Action.php:167
$storeManager
_goBack($backUrl=null)
Definition: Cart.php:73
__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)
Definition: Cart.php:50