Cart.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Controller;
  7. use Magento\Catalog\Controller\Product\View\ViewInterface;
  8. use Magento\Checkout\Model\Cart as CustomerCart;
  9. /**
  10. * Shopping cart controller
  11. */
  12. abstract class Cart extends \Magento\Framework\App\Action\Action implements ViewInterface
  13. {
  14. /**
  15. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  16. */
  17. protected $_scopeConfig;
  18. /**
  19. * @var \Magento\Checkout\Model\Session
  20. */
  21. protected $_checkoutSession;
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface
  24. */
  25. protected $_storeManager;
  26. /**
  27. * @var \Magento\Framework\Data\Form\FormKey\Validator
  28. */
  29. protected $_formKeyValidator;
  30. /**
  31. * @var \Magento\Checkout\Model\Cart
  32. */
  33. protected $cart;
  34. /**
  35. * @param \Magento\Framework\App\Action\Context $context
  36. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  37. * @param \Magento\Checkout\Model\Session $checkoutSession
  38. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  39. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  40. * @param CustomerCart $cart
  41. * @codeCoverageIgnore
  42. */
  43. public function __construct(
  44. \Magento\Framework\App\Action\Context $context,
  45. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  46. \Magento\Checkout\Model\Session $checkoutSession,
  47. \Magento\Store\Model\StoreManagerInterface $storeManager,
  48. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  49. CustomerCart $cart
  50. ) {
  51. $this->_formKeyValidator = $formKeyValidator;
  52. $this->_scopeConfig = $scopeConfig;
  53. $this->_checkoutSession = $checkoutSession;
  54. $this->_storeManager = $storeManager;
  55. $this->cart = $cart;
  56. parent::__construct($context);
  57. }
  58. /**
  59. * Set back redirect url to response
  60. *
  61. * @param null|string $backUrl
  62. *
  63. * @return \Magento\Framework\Controller\Result\Redirect
  64. */
  65. protected function _goBack($backUrl = null)
  66. {
  67. $resultRedirect = $this->resultRedirectFactory->create();
  68. if ($backUrl || $backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) {
  69. $resultRedirect->setUrl($backUrl);
  70. }
  71. return $resultRedirect;
  72. }
  73. /**
  74. * Check if URL corresponds store
  75. *
  76. * @param string $url
  77. * @return bool
  78. */
  79. protected function _isInternalUrl($url)
  80. {
  81. if (strpos($url, 'http') === false) {
  82. return false;
  83. }
  84. /**
  85. * Url must start from base secure or base unsecure url
  86. */
  87. /** @var $store \Magento\Store\Model\Store */
  88. $store = $this->_storeManager->getStore();
  89. $unsecure = strpos($url, $store->getBaseUrl()) === 0;
  90. $secure = strpos($url, $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, true)) === 0;
  91. return $unsecure || $secure;
  92. }
  93. /**
  94. * Get resolved back url
  95. *
  96. * @param string|null $defaultUrl
  97. * @return mixed|null|string
  98. */
  99. protected function getBackUrl($defaultUrl = null)
  100. {
  101. $returnUrl = $this->getRequest()->getParam('return_url');
  102. if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
  103. $this->messageManager->getMessages()->clear();
  104. return $returnUrl;
  105. }
  106. if ($this->shouldRedirectToCart() || $this->getRequest()->getParam('in_cart')) {
  107. if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
  108. $this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
  109. }
  110. return $this->_url->getUrl('checkout/cart');
  111. }
  112. return $defaultUrl;
  113. }
  114. /**
  115. * Is redirect should be performed after the product was added to cart.
  116. *
  117. * @return bool
  118. */
  119. private function shouldRedirectToCart()
  120. {
  121. return $this->_scopeConfig->isSetFlag(
  122. 'checkout/cart/redirect_to_cart',
  123. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  124. );
  125. }
  126. }