Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cart.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  protected $checkoutSession;
20 
24  protected $checkoutCart;
25 
29  protected $catalogUrl;
30 
34  protected $quote = null;
35 
39  protected $checkoutHelper;
40 
44  protected $itemPoolInterface;
45 
49  protected $summeryCount;
50 
54  protected $layout;
55 
66  public function __construct(
67  \Magento\Checkout\Model\Session $checkoutSession,
68  \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
69  \Magento\Checkout\Model\Cart $checkoutCart,
70  \Magento\Checkout\Helper\Data $checkoutHelper,
72  \Magento\Framework\View\LayoutInterface $layout,
73  array $data = []
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  }
83 
87  public function getSectionData()
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  }
104 
110  protected function getQuote()
111  {
112  if (null === $this->quote) {
113  $this->quote = $this->checkoutSession->getQuote();
114  }
115  return $this->quote;
116  }
117 
123  protected function getSummaryCount()
124  {
125  if (!$this->summeryCount) {
126  $this->summeryCount = $this->checkoutCart->getSummaryQty() ?: 0;
127  }
128  return $this->summeryCount;
129  }
130 
136  protected function isPossibleOnepageCheckout()
137  {
138  return $this->checkoutHelper->canOnepageCheckout() && !$this->getQuote()->getHasError();
139  }
140 
146  protected function getRecentItems()
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  }
171 
177  protected function getAllQuoteItems()
178  {
179  if ($this->getCustomQuote()) {
180  return $this->getCustomQuote()->getAllVisibleItems();
181  }
182  return $this->getQuote()->getAllVisibleItems();
183  }
184 
190  public function isGuestCheckoutAllowed()
191  {
192  return $this->checkoutHelper->isAllowedGuestCheckout($this->checkoutSession->getQuote());
193  }
194 }
__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=[])
Definition: Cart.php:66
$totals
Definition: totalbar.phtml:10
$items