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