Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCart.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  const DEFAULT_TYPE = 'default';
19 
23  protected $_quote = null;
24 
28  protected $_totals;
29 
33  protected $_itemRenders = [];
34 
40  protected $_customerSession;
41 
45  protected $_checkoutSession;
46 
54  public function __construct(
55  \Magento\Framework\View\Element\Template\Context $context,
56  \Magento\Customer\Model\Session $customerSession,
57  \Magento\Checkout\Model\Session $checkoutSession,
58  array $data = []
59  ) {
60  $this->_customerSession = $customerSession;
61  $this->_checkoutSession = $checkoutSession;
62  parent::__construct($context, $data);
63  $this->_isScopePrivate = true;
64  }
65 
71  protected function _getRendererList()
72  {
73  return $this->getRendererListName() ? $this->getLayout()->getBlock(
74  $this->getRendererListName()
75  ) : $this->getChildBlock(
76  'renderer.list'
77  );
78  }
79 
87  public function getItemRenderer($type = null)
88  {
89  if ($type === null) {
91  }
92  $rendererList = $this->_getRendererList();
93  if (!$rendererList) {
94  throw new \RuntimeException('Renderer list for block "' . $this->getNameInLayout() . '" is not defined');
95  }
96  $overriddenTemplates = $this->getOverriddenTemplates() ?: [];
97  $template = isset($overriddenTemplates[$type]) ? $overriddenTemplates[$type] : $this->getRendererTemplate();
98  return $rendererList->getRenderer($type, self::DEFAULT_TYPE, $template);
99  }
100 
106  public function getQuote()
107  {
108  if (null === $this->_quote) {
109  $this->_quote = $this->_checkoutSession->getQuote();
110  }
111  return $this->_quote;
112  }
113 
120  public function getItems()
121  {
122  return $this->getQuote()->getAllVisibleItems();
123  }
124 
131  public function getItemHtml(\Magento\Quote\Model\Quote\Item $item)
132  {
133  $renderer = $this->getItemRenderer($item->getProductType())->setItem($item);
134  return $renderer->toHtml();
135  }
136 
141  public function getTotals()
142  {
143  return $this->getTotalsCache();
144  }
145 
149  public function getTotalsCache()
150  {
151  if (empty($this->_totals)) {
152  if ($this->getQuote()->isVirtual()) {
153  $this->_totals = $this->getQuote()->getBillingAddress()->getTotals();
154  } else {
155  $this->_totals = $this->getQuote()->getShippingAddress()->getTotals();
156  }
157  }
158  return $this->_totals;
159  }
160 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, array $data=[])
getItemHtml(\Magento\Quote\Model\Quote\Item $item)
$type
Definition: item.phtml:13
$template
Definition: export.php:12