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 
13 class Cart extends \Magento\Framework\Url\Helper\Data
14 {
18  const DELETE_URL = 'checkout/cart/delete';
19 
23  const XML_PATH_REDIRECT_TO_CART = 'checkout/cart/redirect_to_cart';
24 
29 
33  protected $_checkoutCart;
34 
38  protected $_checkoutSession;
39 
46  public function __construct(
47  \Magento\Framework\App\Helper\Context $context,
48  \Magento\Checkout\Model\Cart $checkoutCart,
49  \Magento\Checkout\Model\Session $checkoutSession
50  ) {
51  $this->_checkoutCart = $checkoutCart;
52  $this->_checkoutSession = $checkoutSession;
53  parent::__construct($context);
54  }
55 
62  public function getCart()
63  {
64  return $this->_checkoutCart;
65  }
66 
74  public function getAddUrl($product, $additional = [])
75  {
76  if (isset($additional['useUencPlaceholder'])) {
77  $uenc = "%uenc%";
78  unset($additional['useUencPlaceholder']);
79  } else {
80  $uenc = $this->urlEncoder->encode($this->_urlBuilder->getCurrentUrl());
81  }
82 
84 
85  $routeParams = [
86  $urlParamName => $uenc,
87  'product' => $product->getEntityId(),
88  '_secure' => $this->_getRequest()->isSecure()
89  ];
90 
91  if (!empty($additional)) {
92  $routeParams = array_merge($routeParams, $additional);
93  }
94 
95  if ($product->hasUrlDataObject()) {
96  $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId();
97  $routeParams['_scope_to_url'] = true;
98  }
99 
100  if ($this->_getRequest()->getRouteName() == 'checkout'
101  && $this->_getRequest()->getControllerName() == 'cart'
102  ) {
103  $routeParams['in_cart'] = 1;
104  }
105 
106  return $this->_getUrl('checkout/cart/add', $routeParams);
107  }
108 
115  public function getRemoveUrl($item)
116  {
117  $params = [
118  'id' => $item->getId(),
120  ];
121  return $this->_getUrl(self::DELETE_URL, $params);
122  }
123 
130  public function getDeletePostJson($item)
131  {
132  $url = $this->_getUrl(self::DELETE_URL);
133 
134  $data = ['id' => $item->getId()];
135  if (!$this->_request->isAjax()) {
137  }
138  return json_encode(['action' => $url, 'data' => $data]);
139  }
140 
147  public function getCartUrl()
148  {
149  return $this->_getUrl('checkout/cart');
150  }
151 
158  public function getQuote()
159  {
160  return $this->_checkoutSession->getQuote();
161  }
162 
169  public function getItemsCount()
170  {
171  return $this->getCart()->getItemsCount();
172  }
173 
180  public function getItemsQty()
181  {
182  return $this->getCart()->getItemsQty();
183  }
184 
191  public function getSummaryCount()
192  {
193  return $this->getCart()->getSummaryQty();
194  }
195 
203  public function getIsVirtualQuote()
204  {
205  return $this->getQuote()->isVirtual();
206  }
207 
216  public function getShouldRedirectToCart($store = null)
217  {
218  return $this->scopeConfig->isSetFlag(
219  self::XML_PATH_REDIRECT_TO_CART,
221  $store
222  );
223  }
224 }
getAddUrl($product, $additional=[])
Definition: Cart.php:74
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Checkout\Model\Cart $checkoutCart, \Magento\Checkout\Model\Session $checkoutSession)
Definition: Cart.php:46
getShouldRedirectToCart($store=null)
Definition: Cart.php:216
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18