Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Cart Class Reference
Inheritance diagram for Cart:
Data AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Checkout\Model\Cart $checkoutCart, \Magento\Checkout\Model\Session $checkoutSession)
 
 getCart ()
 
 getAddUrl ($product, $additional=[])
 
 getRemoveUrl ($item)
 
 getDeletePostJson ($item)
 
 getCartUrl ()
 
 getQuote ()
 
 getItemsCount ()
 
 getItemsQty ()
 
 getSummaryCount ()
 
 getIsVirtualQuote ()
 
 getShouldRedirectToCart ($store=null)
 
- Public Member Functions inherited from Data
 getCurrentBase64Url ()
 
 getEncodedUrl ($url=null)
 
 addRequestParam ($url, $param)
 
 removeRequestParam ($url, $paramKey, $caseSensitive=false)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const DELETE_URL = 'checkout/cart/delete'
 
const XML_PATH_REDIRECT_TO_CART = 'checkout/cart/redirect_to_cart'
 
const COUPON_CODE_MAX_LENGTH = 255
 

Protected Attributes

 $_checkoutCart
 
 $_checkoutSession
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Detailed Description

Shopping cart helper

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m

Definition at line 13 of file Cart.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Helper\Context  $context,
\Magento\Checkout\Model\Cart  $checkoutCart,
\Magento\Checkout\Model\Session  $checkoutSession 
)
Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Checkout\Model\Cart$checkoutCart
\Magento\Checkout\Model\Session$checkoutSession@codeCoverageIgnore

Definition at line 46 of file Cart.php.

50  {
51  $this->_checkoutCart = $checkoutCart;
52  $this->_checkoutSession = $checkoutSession;
53  parent::__construct($context);
54  }

Member Function Documentation

◆ getAddUrl()

getAddUrl (   $product,
  $additional = [] 
)

Retrieve url for add product to cart

Parameters
\Magento\Catalog\Model\Product$product
array$additional
Returns
string

Definition at line 74 of file Cart.php.

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  }

◆ getCart()

getCart ( )

Retrieve cart instance

Returns
\Magento\Checkout\Model\Cart @codeCoverageIgnore

Definition at line 62 of file Cart.php.

63  {
64  return $this->_checkoutCart;
65  }

◆ getCartUrl()

getCartUrl ( )

Retrieve shopping cart url

Returns
string @codeCoverageIgnore

Definition at line 147 of file Cart.php.

148  {
149  return $this->_getUrl('checkout/cart');
150  }

◆ getDeletePostJson()

getDeletePostJson (   $item)

Get post parameters for delete from cart

Parameters
\Magento\Quote\Model\Quote\Item\AbstractItem$item
Returns
string

Definition at line 130 of file Cart.php.

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  }

◆ getIsVirtualQuote()

getIsVirtualQuote ( )

Check quote for virtual products only

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName) @codeCoverageIgnore

Definition at line 203 of file Cart.php.

204  {
205  return $this->getQuote()->isVirtual();
206  }

◆ getItemsCount()

getItemsCount ( )

Get shopping cart items count

Returns
int @codeCoverageIgnore

Definition at line 169 of file Cart.php.

170  {
171  return $this->getCart()->getItemsCount();
172  }

◆ getItemsQty()

getItemsQty ( )

Get shopping cart summary qty

Returns
int|float @codeCoverageIgnore

Definition at line 180 of file Cart.php.

181  {
182  return $this->getCart()->getItemsQty();
183  }

◆ getQuote()

getQuote ( )

Retrieve current quote instance

Returns
\Magento\Quote\Model\Quote @codeCoverageIgnore

Definition at line 158 of file Cart.php.

159  {
160  return $this->_checkoutSession->getQuote();
161  }

◆ getRemoveUrl()

getRemoveUrl (   $item)

Retrieve url for remove product from cart

Parameters
\Magento\Quote\Model\Quote\Item\AbstractItem$item
Returns
string

Definition at line 115 of file Cart.php.

116  {
117  $params = [
118  'id' => $item->getId(),
120  ];
121  return $this->_getUrl(self::DELETE_URL, $params);
122  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getShouldRedirectToCart()

getShouldRedirectToCart (   $store = null)

Checks if customer should be redirected to shopping cart after adding a product

Parameters
int | string | \Magento\Store\Model\Store$store
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName) @codeCoverageIgnore

Definition at line 216 of file Cart.php.

217  {
218  return $this->scopeConfig->isSetFlag(
219  self::XML_PATH_REDIRECT_TO_CART,
221  $store
222  );
223  }

◆ getSummaryCount()

getSummaryCount ( )

Get shopping cart items summary (include config settings)

Returns
int|float @codeCoverageIgnore

Definition at line 191 of file Cart.php.

192  {
193  return $this->getCart()->getSummaryQty();
194  }

Field Documentation

◆ $_checkoutCart

$_checkoutCart
protected

Definition at line 33 of file Cart.php.

◆ $_checkoutSession

$_checkoutSession
protected

Definition at line 38 of file Cart.php.

◆ COUPON_CODE_MAX_LENGTH

const COUPON_CODE_MAX_LENGTH = 255

Maximal coupon code length according to database table definitions (longer codes are truncated)

Definition at line 28 of file Cart.php.

◆ DELETE_URL

const DELETE_URL = 'checkout/cart/delete'

Path to controller to delete item from cart

Definition at line 18 of file Cart.php.

◆ XML_PATH_REDIRECT_TO_CART

const XML_PATH_REDIRECT_TO_CART = 'checkout/cart/redirect_to_cart'

Path for redirect to cart

Definition at line 23 of file Cart.php.


The documentation for this class was generated from the following file: