Cart.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Helper;
  7. /**
  8. * Shopping cart helper
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Cart extends \Magento\Framework\Url\Helper\Data
  13. {
  14. /**
  15. * Path to controller to delete item from cart
  16. */
  17. const DELETE_URL = 'checkout/cart/delete';
  18. /**
  19. * Path for redirect to cart
  20. */
  21. const XML_PATH_REDIRECT_TO_CART = 'checkout/cart/redirect_to_cart';
  22. /**
  23. * Maximal coupon code length according to database table definitions (longer codes are truncated)
  24. */
  25. const COUPON_CODE_MAX_LENGTH = 255;
  26. /**
  27. * @var \Magento\Checkout\Model\Cart
  28. */
  29. protected $_checkoutCart;
  30. /**
  31. * @var \Magento\Checkout\Model\Session
  32. */
  33. protected $_checkoutSession;
  34. /**
  35. * @param \Magento\Framework\App\Helper\Context $context
  36. * @param \Magento\Checkout\Model\Cart $checkoutCart
  37. * @param \Magento\Checkout\Model\Session $checkoutSession
  38. * @codeCoverageIgnore
  39. */
  40. public function __construct(
  41. \Magento\Framework\App\Helper\Context $context,
  42. \Magento\Checkout\Model\Cart $checkoutCart,
  43. \Magento\Checkout\Model\Session $checkoutSession
  44. ) {
  45. $this->_checkoutCart = $checkoutCart;
  46. $this->_checkoutSession = $checkoutSession;
  47. parent::__construct($context);
  48. }
  49. /**
  50. * Retrieve cart instance
  51. *
  52. * @return \Magento\Checkout\Model\Cart
  53. * @codeCoverageIgnore
  54. */
  55. public function getCart()
  56. {
  57. return $this->_checkoutCart;
  58. }
  59. /**
  60. * Retrieve url for add product to cart
  61. *
  62. * @param \Magento\Catalog\Model\Product $product
  63. * @param array $additional
  64. * @return string
  65. */
  66. public function getAddUrl($product, $additional = [])
  67. {
  68. if (isset($additional['useUencPlaceholder'])) {
  69. $uenc = "%uenc%";
  70. unset($additional['useUencPlaceholder']);
  71. } else {
  72. $uenc = $this->urlEncoder->encode($this->_urlBuilder->getCurrentUrl());
  73. }
  74. $urlParamName = \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED;
  75. $routeParams = [
  76. $urlParamName => $uenc,
  77. 'product' => $product->getEntityId(),
  78. '_secure' => $this->_getRequest()->isSecure()
  79. ];
  80. if (!empty($additional)) {
  81. $routeParams = array_merge($routeParams, $additional);
  82. }
  83. if ($product->hasUrlDataObject()) {
  84. $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId();
  85. $routeParams['_scope_to_url'] = true;
  86. }
  87. if ($this->_getRequest()->getRouteName() == 'checkout'
  88. && $this->_getRequest()->getControllerName() == 'cart'
  89. ) {
  90. $routeParams['in_cart'] = 1;
  91. }
  92. return $this->_getUrl('checkout/cart/add', $routeParams);
  93. }
  94. /**
  95. * Retrieve url for remove product from cart
  96. *
  97. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  98. * @return string
  99. */
  100. public function getRemoveUrl($item)
  101. {
  102. $params = [
  103. 'id' => $item->getId(),
  104. \Magento\Framework\App\ActionInterface::PARAM_NAME_BASE64_URL => $this->getCurrentBase64Url(),
  105. ];
  106. return $this->_getUrl(self::DELETE_URL, $params);
  107. }
  108. /**
  109. * Get post parameters for delete from cart
  110. *
  111. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  112. * @return string
  113. */
  114. public function getDeletePostJson($item)
  115. {
  116. $url = $this->_getUrl(self::DELETE_URL);
  117. $data = ['id' => $item->getId()];
  118. if (!$this->_request->isAjax()) {
  119. $data[\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED] = $this->getCurrentBase64Url();
  120. }
  121. return json_encode(['action' => $url, 'data' => $data]);
  122. }
  123. /**
  124. * Retrieve shopping cart url
  125. *
  126. * @return string
  127. * @codeCoverageIgnore
  128. */
  129. public function getCartUrl()
  130. {
  131. return $this->_getUrl('checkout/cart');
  132. }
  133. /**
  134. * Retrieve current quote instance
  135. *
  136. * @return \Magento\Quote\Model\Quote
  137. * @codeCoverageIgnore
  138. */
  139. public function getQuote()
  140. {
  141. return $this->_checkoutSession->getQuote();
  142. }
  143. /**
  144. * Get shopping cart items count
  145. *
  146. * @return int
  147. * @codeCoverageIgnore
  148. */
  149. public function getItemsCount()
  150. {
  151. return $this->getCart()->getItemsCount();
  152. }
  153. /**
  154. * Get shopping cart summary qty
  155. *
  156. * @return int|float
  157. * @codeCoverageIgnore
  158. */
  159. public function getItemsQty()
  160. {
  161. return $this->getCart()->getItemsQty();
  162. }
  163. /**
  164. * Get shopping cart items summary (include config settings)
  165. *
  166. * @return int|float
  167. * @codeCoverageIgnore
  168. */
  169. public function getSummaryCount()
  170. {
  171. return $this->getCart()->getSummaryQty();
  172. }
  173. /**
  174. * Check quote for virtual products only
  175. *
  176. * @return bool
  177. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  178. * @codeCoverageIgnore
  179. */
  180. public function getIsVirtualQuote()
  181. {
  182. return $this->getQuote()->isVirtual();
  183. }
  184. /**
  185. * Checks if customer should be redirected to shopping cart after adding a product
  186. *
  187. * @param int|string|\Magento\Store\Model\Store $store
  188. * @return bool
  189. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  190. * @codeCoverageIgnore
  191. */
  192. public function getShouldRedirectToCart($store = null)
  193. {
  194. return $this->scopeConfig->isSetFlag(
  195. self::XML_PATH_REDIRECT_TO_CART,
  196. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  197. $store
  198. );
  199. }
  200. }