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 
9 class Cart
10 {
14  protected $checkoutSession;
15 
19  protected $checkoutHelper;
20 
24  protected $itemPriceRenderer;
25 
29  protected $quote = null;
30 
34  protected $totals = null;
35 
41  public function __construct(
42  \Magento\Checkout\Model\Session $checkoutSession,
43  \Magento\Checkout\Helper\Data $checkoutHelper,
44  \Magento\Tax\Block\Item\Price\Renderer $itemPriceRenderer
45  ) {
46  $this->checkoutSession = $checkoutSession;
47  $this->checkoutHelper = $checkoutHelper;
48  $this->itemPriceRenderer = $itemPriceRenderer;
49  }
50 
59  public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
60  {
61  $result['subtotal_incl_tax'] = $this->checkoutHelper->formatPrice($this->getSubtotalInclTax());
62  $result['subtotal_excl_tax'] = $this->checkoutHelper->formatPrice($this->getSubtotalExclTax());
63 
64  $items =$this->getQuote()->getAllVisibleItems();
65  if (is_array($result['items'])) {
66  foreach ($result['items'] as $key => $itemAsArray) {
67  if ($item = $this->findItemById($itemAsArray['item_id'], $items)) {
68  $this->itemPriceRenderer->setItem($item);
69  $this->itemPriceRenderer->setTemplate('checkout/cart/item/price/sidebar.phtml');
70  $result['items'][$key]['product_price']=$this->itemPriceRenderer->toHtml();
71  }
72  }
73  }
74  return $result;
75  }
76 
82  protected function getSubtotalInclTax()
83  {
84  $subtotal = 0;
85  $totals = $this->getTotals();
86  if (isset($totals['subtotal'])) {
87  $subtotal = $totals['subtotal']->getValueInclTax() ?: $totals['subtotal']->getValue();
88  }
89  return $subtotal;
90  }
91 
97  protected function getSubtotalExclTax()
98  {
99  $subtotal = 0;
100  $totals = $this->getTotals();
101  if (isset($totals['subtotal'])) {
102  $subtotal = $totals['subtotal']->getValueExclTax() ?: $totals['subtotal']->getValue();
103  }
104  return $subtotal;
105  }
106 
112  public function getTotals()
113  {
114  // TODO: TODO: MAGETWO-34824 duplicate \Magento\Checkout\CustomerData\Cart::getSectionData
115  if (empty($this->totals)) {
116  $this->totals = $this->getQuote()->getTotals();
117  }
118  return $this->totals;
119  }
120 
126  protected function getQuote()
127  {
128  if (null === $this->quote) {
129  $this->quote = $this->checkoutSession->getQuote();
130  }
131  return $this->quote;
132  }
133 
141  protected function findItemById($id, $itemsHaystack)
142  {
143  if (is_array($itemsHaystack)) {
144  foreach ($itemsHaystack as $item) {
146  if ((int)$item->getItemId() == $id) {
147  return $item;
148  }
149  }
150  }
151  return false;
152  }
153 }
$id
Definition: fieldset.phtml:14
afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
Definition: Cart.php:59
__construct(\Magento\Checkout\Model\Session $checkoutSession, \Magento\Checkout\Helper\Data $checkoutHelper, \Magento\Tax\Block\Item\Price\Renderer $itemPriceRenderer)
Definition: Cart.php:41
$items