Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sidebar.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Checkout\Model;
7 
8 use Magento\Checkout\Helper\Data as HelperData;
14 
18 class Sidebar
19 {
23  protected $cart;
24 
28  protected $helperData;
29 
33  protected $resolver;
34 
38  protected $summaryQty;
39 
46  public function __construct(
47  Cart $cart,
48  HelperData $helperData,
50  ) {
51  $this->cart = $cart;
52  $this->helperData = $helperData;
53  $this->resolver = $resolver;
54  }
55 
62  public function getResponseData($error = '')
63  {
64  if (empty($error)) {
65  $response = [
66  'success' => true,
67  ];
68  } else {
69  $response = [
70  'success' => false,
71  'error_message' => $error,
72  ];
73  }
74  return $response;
75  }
76 
84  public function checkQuoteItem($itemId)
85  {
86  $item = $this->cart->getQuote()->getItemById($itemId);
87  if (!$item instanceof CartItemInterface) {
88  throw new LocalizedException(__("The quote item isn't found. Verify the item and try again."));
89  }
90  return $this;
91  }
92 
99  public function removeQuoteItem($itemId)
100  {
101  $this->cart->removeItem($itemId);
102  $this->cart->save();
103  return $this;
104  }
105 
114  public function updateQuoteItem($itemId, $itemQty)
115  {
116  $itemData = [$itemId => ['qty' => $this->normalize($itemQty)]];
117  $this->cart->updateItems($itemData)->save();
118  return $this;
119  }
120 
127  protected function normalize($itemQty)
128  {
129  if ($itemQty) {
130  $filter = new \Zend_Filter_LocalizedToNormalized(
131  ['locale' => $this->resolver->getLocale()]
132  );
133  return $filter->filter($itemQty);
134  }
135  return $itemQty;
136  }
137 
143  protected function getSummaryQty()
144  {
145  if (!$this->summaryQty) {
146  $this->summaryQty = $this->cart->getSummaryQty();
147  }
148  return $this->summaryQty;
149  }
150 
156  protected function getSummaryText()
157  {
158  return ($this->getSummaryQty() == 1) ? __(' item') : __(' items');
159  }
160 
166  protected function getSubtotalHtml()
167  {
168  $totals = $this->cart->getQuote()->getTotals();
169  $subtotal = isset($totals['subtotal']) && $totals['subtotal'] instanceof Total
170  ? $totals['subtotal']->getValue()
171  : 0;
172  return $this->helperData->formatPrice($subtotal);
173  }
174 }
$response
Definition: 404.php:11
updateQuoteItem($itemId, $itemQty)
Definition: Sidebar.php:114
__()
Definition: __.php:13
$totals
Definition: totalbar.phtml:10
__construct(Cart $cart, HelperData $helperData, ResolverInterface $resolver)
Definition: Sidebar.php:46