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:
DataObject SectionSourceInterface

Public Member Functions

 __construct (\Magento\Checkout\Model\Session $checkoutSession, \Magento\Catalog\Model\ResourceModel\Url $catalogUrl, \Magento\Checkout\Model\Cart $checkoutCart, \Magento\Checkout\Helper\Data $checkoutHelper, ItemPoolInterface $itemPoolInterface, \Magento\Framework\View\LayoutInterface $layout, array $data=[])
 
 getSectionData ()
 
 isGuestCheckoutAllowed ()
 
- 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)
 

Protected Member Functions

 getQuote ()
 
 getSummaryCount ()
 
 isPossibleOnepageCheckout ()
 
 getRecentItems ()
 
 getAllQuoteItems ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $checkoutSession
 
 $checkoutCart
 
 $catalogUrl
 
 $quote = null
 
 $checkoutHelper
 
 $itemPoolInterface
 
 $summeryCount
 
 $layout
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Cart source

Definition at line 14 of file Cart.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Checkout\Model\Session  $checkoutSession,
\Magento\Catalog\Model\ResourceModel\Url  $catalogUrl,
\Magento\Checkout\Model\Cart  $checkoutCart,
\Magento\Checkout\Helper\Data  $checkoutHelper,
ItemPoolInterface  $itemPoolInterface,
\Magento\Framework\View\LayoutInterface  $layout,
array  $data = [] 
)
Parameters
\Magento\Checkout\Model\Session$checkoutSession
\Magento\Catalog\Model\ResourceModel\Url$catalogUrl
\Magento\Checkout\Model\Cart$checkoutCart
\Magento\Checkout\Helper\Data$checkoutHelper
ItemPoolInterface$itemPoolInterface
\Magento\Framework\View\LayoutInterface$layout
array$data@codeCoverageIgnore

Definition at line 66 of file Cart.php.

74  {
75  parent::__construct($data);
76  $this->checkoutSession = $checkoutSession;
77  $this->catalogUrl = $catalogUrl;
78  $this->checkoutCart = $checkoutCart;
79  $this->checkoutHelper = $checkoutHelper;
80  $this->itemPoolInterface = $itemPoolInterface;
81  $this->layout = $layout;
82  }

Member Function Documentation

◆ getAllQuoteItems()

getAllQuoteItems ( )
protected

Return customer quote items

Returns
\Magento\Quote\Model\Quote\Item[]

Definition at line 177 of file Cart.php.

178  {
179  if ($this->getCustomQuote()) {
180  return $this->getCustomQuote()->getAllVisibleItems();
181  }
182  return $this->getQuote()->getAllVisibleItems();
183  }

◆ getQuote()

getQuote ( )
protected

Get active quote

Returns
\Magento\Quote\Model\Quote

Definition at line 110 of file Cart.php.

111  {
112  if (null === $this->quote) {
113  $this->quote = $this->checkoutSession->getQuote();
114  }
115  return $this->quote;
116  }

◆ getRecentItems()

getRecentItems ( )
protected

Get array of last added items

Returns
\Magento\Quote\Model\Quote\Item[]

Definition at line 146 of file Cart.php.

147  {
148  $items = [];
149  if (!$this->getSummaryCount()) {
150  return $items;
151  }
152 
153  foreach (array_reverse($this->getAllQuoteItems()) as $item) {
154  /* @var $item \Magento\Quote\Model\Quote\Item */
155  if (!$item->getProduct()->isVisibleInSiteVisibility()) {
156  $product = $item->getOptionByCode('product_type') !== null
157  ? $item->getOptionByCode('product_type')->getProduct()
158  : $item->getProduct();
159 
160  $products = $this->catalogUrl->getRewriteByProductStore([$product->getId() => $item->getStoreId()]);
161  if (!isset($products[$product->getId()])) {
162  continue;
163  }
164  $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]);
165  $item->getProduct()->setUrlDataObject($urlDataObject);
166  }
167  $items[] = $this->itemPoolInterface->getItemData($item);
168  }
169  return $items;
170  }
$items

◆ getSectionData()

getSectionData ( )

{Get data

Returns
array
}

Implements SectionSourceInterface.

Definition at line 87 of file Cart.php.

88  {
89  $totals = $this->getQuote()->getTotals();
90  $subtotalAmount = $totals['subtotal']->getValue();
91  return [
92  'summary_count' => $this->getSummaryCount(),
93  'subtotalAmount' => $subtotalAmount,
94  'subtotal' => isset($totals['subtotal'])
95  ? $this->checkoutHelper->formatPrice($subtotalAmount)
96  : 0,
97  'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
98  'items' => $this->getRecentItems(),
99  'extra_actions' => $this->layout->createBlock(\Magento\Catalog\Block\ShortcutButtons::class)->toHtml(),
100  'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
101  'website_id' => $this->getQuote()->getStore()->getWebsiteId()
102  ];
103  }
$totals
Definition: totalbar.phtml:10

◆ getSummaryCount()

getSummaryCount ( )
protected

Get shopping cart items qty based on configuration (summary qty or items qty)

Returns
int|float

Definition at line 123 of file Cart.php.

124  {
125  if (!$this->summeryCount) {
126  $this->summeryCount = $this->checkoutCart->getSummaryQty() ?: 0;
127  }
128  return $this->summeryCount;
129  }

◆ isGuestCheckoutAllowed()

isGuestCheckoutAllowed ( )

Check if guest checkout is allowed

Returns
bool

Definition at line 190 of file Cart.php.

191  {
192  return $this->checkoutHelper->isAllowedGuestCheckout($this->checkoutSession->getQuote());
193  }

◆ isPossibleOnepageCheckout()

isPossibleOnepageCheckout ( )
protected

Check if one page checkout is available

Returns
bool

Definition at line 136 of file Cart.php.

137  {
138  return $this->checkoutHelper->canOnepageCheckout() && !$this->getQuote()->getHasError();
139  }

Field Documentation

◆ $catalogUrl

$catalogUrl
protected

Definition at line 29 of file Cart.php.

◆ $checkoutCart

$checkoutCart
protected

Definition at line 24 of file Cart.php.

◆ $checkoutHelper

$checkoutHelper
protected

Definition at line 39 of file Cart.php.

◆ $checkoutSession

$checkoutSession
protected

Definition at line 19 of file Cart.php.

◆ $itemPoolInterface

$itemPoolInterface
protected

Definition at line 44 of file Cart.php.

◆ $layout

$layout
protected

Definition at line 54 of file Cart.php.

◆ $quote

$quote = null
protected

Definition at line 34 of file Cart.php.

◆ $summeryCount

$summeryCount
protected

Definition at line 49 of file Cart.php.


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