Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Cart Class Reference
Inheritance diagram for Cart:
AbstractCart Template AbstractBlock DataObject BlockInterface Grid

Public Member Functions

 __construct (\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder, \Magento\Checkout\Helper\Cart $cartHelper, \Magento\Framework\App\Http\Context $httpContext, array $data=[])
 
 prepareItemUrls ()
 
 hasError ()
 
 getItemsSummaryQty ()
 
 isWishlistActive ()
 
 getCheckoutUrl ()
 
 getContinueShoppingUrl ()
 
 getIsVirtual ()
 
 getMethods ($alias)
 
 getMethodHtml ($name)
 
 getItems ()
 
 getItemsCount ()
 
 getPagerHtml ()
 
- Public Member Functions inherited from AbstractCart
 __construct (\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, array $data=[])
 
 getItemRenderer ($type=null)
 
 getQuote ()
 
 getItems ()
 
 getItemHtml (\Magento\Quote\Model\Quote\Item $item)
 
 getTotals ()
 
 getTotalsCache ()
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Member Functions

 _construct ()
 
- Protected Member Functions inherited from AbstractCart
 _getRendererList ()
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_catalogUrlBuilder
 
 $httpContext
 
 $_cartHelper
 
- Protected Attributes inherited from AbstractCart
 $_quote = null
 
 $_totals
 
 $_itemRenders = []
 
 $_customerSession
 
 $_checkoutSession
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from AbstractCart
const DEFAULT_TYPE = 'default'
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Shopping cart block

@api

Since
100.0.2

Definition at line 16 of file Cart.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\View\Element\Template\Context  $context,
\Magento\Customer\Model\Session  $customerSession,
\Magento\Checkout\Model\Session  $checkoutSession,
\Magento\Catalog\Model\ResourceModel\Url  $catalogUrlBuilder,
\Magento\Checkout\Helper\Cart  $cartHelper,
\Magento\Framework\App\Http\Context  $httpContext,
array  $data = [] 
)
Parameters
\Magento\Framework\View\Element\Template\Context$context
\Magento\Customer\Model\Session$customerSession
\Magento\Checkout\Model\Session$checkoutSession
\Magento\Catalog\Model\ResourceModel\Url$catalogUrlBuilder
\Magento\Checkout\Helper\Cart$cartHelper
\Magento\Framework\App\Http\Context$httpContext
array$data@codeCoverageIgnore

Definition at line 43 of file Cart.php.

51  {
52  $this->_cartHelper = $cartHelper;
53  $this->_catalogUrlBuilder = $catalogUrlBuilder;
54  parent::__construct($context, $customerSession, $checkoutSession, $data);
55  $this->_isScopePrivate = true;
56  $this->httpContext = $httpContext;
57  }

Member Function Documentation

◆ _construct()

_construct ( )
protected

Prepare Quote Item Product URLs

@codeCoverageIgnore

Returns
void

Definition at line 65 of file Cart.php.

66  {
67  parent::_construct();
68  $this->prepareItemUrls();
69  }

◆ getCheckoutUrl()

getCheckoutUrl ( )

@codeCoverageIgnore

Returns
string

Definition at line 154 of file Cart.php.

155  {
156  return $this->getUrl('checkout', ['_secure' => true]);
157  }

◆ getContinueShoppingUrl()

getContinueShoppingUrl ( )
Returns
string

Definition at line 162 of file Cart.php.

163  {
164  $url = $this->getData('continue_shopping_url');
165  if ($url === null) {
166  $url = $this->_checkoutSession->getContinueShoppingUrl(true);
167  if (!$url) {
168  $url = $this->_urlBuilder->getUrl();
169  }
170  $this->setData('continue_shopping_url', $url);
171  }
172  return $url;
173  }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72

◆ getIsVirtual()

getIsVirtual ( )
Returns
bool @codeCoverageIgnore @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 180 of file Cart.php.

181  {
182  return $this->_cartHelper->getIsVirtualQuote();
183  }

◆ getItems()

getItems ( )

Return customer quote items

Returns
array

Definition at line 221 of file Cart.php.

222  {
223  if ($this->getCustomItems()) {
224  return $this->getCustomItems();
225  }
226 
227  return parent::getItems();
228  }

◆ getItemsCount()

getItemsCount ( )

@codeCoverageIgnore

Returns
int

Definition at line 234 of file Cart.php.

235  {
236  return $this->getQuote()->getItemsCount();
237  }

◆ getItemsSummaryQty()

getItemsSummaryQty ( )

@codeCoverageIgnore

Returns
int

Definition at line 126 of file Cart.php.

127  {
128  return $this->getQuote()->getItemsSummaryQty();
129  }

◆ getMethodHtml()

getMethodHtml (   $name)

Return HTML of checkout method (link, button etc.)

Parameters
string$nameBlock name in layout
Returns
string
Exceptions

Definition at line 207 of file Cart.php.

208  {
209  $block = $this->getLayout()->getBlock($name);
210  if (!$block) {
211  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid method: %1', $name));
212  }
213  return $block->toHtml();
214  }
__()
Definition: __.php:13
$block
Definition: block.php:8
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getMethods()

getMethods (   $alias)

Return list of available checkout methods

Parameters
string$aliasContainer block alias in layout
Returns
array

Definition at line 191 of file Cart.php.

192  {
193  $childName = $this->getLayout()->getChildName($this->getNameInLayout(), $alias);
194  if ($childName) {
195  return $this->getLayout()->getChildNames($childName);
196  }
197  return [];
198  }
if(!trim($html)) $alias
Definition: details.phtml:20

◆ getPagerHtml()

getPagerHtml ( )

Render pagination HTML

Returns
string
Since
100.1.7

Definition at line 245 of file Cart.php.

246  {
247  return $this->getChildHtml('pager');
248  }

◆ hasError()

hasError ( )

@codeCoverageIgnore

Returns
bool

Definition at line 117 of file Cart.php.

118  {
119  return $this->getQuote()->getHasError();
120  }

◆ isWishlistActive()

isWishlistActive ( )

@codeCoverageIgnore

Returns
bool

Definition at line 135 of file Cart.php.

136  {
137  $isActive = $this->_getData('is_wishlist_active');
138  if ($isActive === null) {
139  $isActive = $this->_scopeConfig->getValue(
140  'wishlist/general/active',
141  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
142  ) && $this->httpContext->getValue(
144  );
145  $this->setIsWishlistActive($isActive);
146  }
147  return $isActive;
148  }

◆ prepareItemUrls()

prepareItemUrls ( )

prepare cart items URLs

Returns
void @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 77 of file Cart.php.

78  {
79  $products = [];
80  /* @var $item \Magento\Quote\Model\Quote\Item */
81  foreach ($this->getItems() as $item) {
82  $product = $item->getProduct();
83  $option = $item->getOptionByCode('product_type');
84  if ($option) {
85  $product = $option->getProduct();
86  }
87 
88  if ($item->getStoreId() != $this->_storeManager->getStore()->getId() &&
89  !$item->getRedirectUrl() &&
90  !$product->isVisibleInSiteVisibility()
91  ) {
92  $products[$product->getId()] = $item->getStoreId();
93  }
94  }
95 
96  if ($products) {
97  $products = $this->_catalogUrlBuilder->getRewriteByProductStore($products);
98  foreach ($this->getItems() as $item) {
99  $product = $item->getProduct();
100  $option = $item->getOptionByCode('product_type');
101  if ($option) {
102  $product = $option->getProduct();
103  }
104 
105  if (isset($products[$product->getId()])) {
106  $object = new \Magento\Framework\DataObject($products[$product->getId()]);
107  $item->getProduct()->setUrlDataObject($object);
108  }
109  }
110  }
111  }

Field Documentation

◆ $_cartHelper

$_cartHelper
protected

Definition at line 31 of file Cart.php.

◆ $_catalogUrlBuilder

$_catalogUrlBuilder
protected

Definition at line 21 of file Cart.php.

◆ $httpContext

$httpContext
protected

Definition at line 26 of file Cart.php.


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