Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Sidebar Class Reference

Public Member Functions

 __construct (Cart $cart, HelperData $helperData, ResolverInterface $resolver)
 
 getResponseData ($error='')
 
 checkQuoteItem ($itemId)
 
 removeQuoteItem ($itemId)
 
 updateQuoteItem ($itemId, $itemQty)
 

Protected Member Functions

 normalize ($itemQty)
 
 getSummaryQty ()
 
 getSummaryText ()
 
 getSubtotalHtml ()
 

Protected Attributes

 $cart
 
 $helperData
 
 $resolver
 
 $summaryQty
 

Detailed Description

Deprecated:
100.1.0

Definition at line 18 of file Sidebar.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Cart  $cart,
HelperData  $helperData,
ResolverInterface  $resolver 
)
Parameters
Cart$cart
HelperData$helperData
ResolverInterface$resolver@codeCoverageIgnore

Definition at line 46 of file Sidebar.php.

50  {
51  $this->cart = $cart;
52  $this->helperData = $helperData;
53  $this->resolver = $resolver;
54  }

Member Function Documentation

◆ checkQuoteItem()

checkQuoteItem (   $itemId)

Check if required quote item exist

Parameters
int$itemId
Exceptions
LocalizedException
Returns
$this

Definition at line 84 of file Sidebar.php.

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  }
__()
Definition: __.php:13

◆ getResponseData()

getResponseData (   $error = '')

Compile response data

Parameters
string$error
Returns
array

Definition at line 62 of file Sidebar.php.

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  }
$response
Definition: 404.php:11

◆ getSubtotalHtml()

getSubtotalHtml ( )
protected

Retrieve subtotal block html

Returns
string

Definition at line 166 of file Sidebar.php.

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  }
$totals
Definition: totalbar.phtml:10

◆ getSummaryQty()

getSummaryQty ( )
protected

Retrieve summary qty

Returns
int

Definition at line 143 of file Sidebar.php.

144  {
145  if (!$this->summaryQty) {
146  $this->summaryQty = $this->cart->getSummaryQty();
147  }
148  return $this->summaryQty;
149  }

◆ getSummaryText()

getSummaryText ( )
protected

Retrieve summary qty text

Returns
string

Definition at line 156 of file Sidebar.php.

157  {
158  return ($this->getSummaryQty() == 1) ? __(' item') : __(' items');
159  }
__()
Definition: __.php:13

◆ normalize()

normalize (   $itemQty)
protected

Apply normalization filter to item qty value

Parameters
int$itemQty
Returns
int|array

Definition at line 127 of file Sidebar.php.

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  }

◆ removeQuoteItem()

removeQuoteItem (   $itemId)

Remove quote item

Parameters
int$itemId
Returns
$this

Definition at line 99 of file Sidebar.php.

100  {
101  $this->cart->removeItem($itemId);
102  $this->cart->save();
103  return $this;
104  }

◆ updateQuoteItem()

updateQuoteItem (   $itemId,
  $itemQty 
)

Update quote item

Parameters
int$itemId
int$itemQty
Exceptions
LocalizedException
Returns
$this

Definition at line 114 of file Sidebar.php.

115  {
116  $itemData = [$itemId => ['qty' => $this->normalize($itemQty)]];
117  $this->cart->updateItems($itemData)->save();
118  return $this;
119  }

Field Documentation

◆ $cart

$cart
protected

Definition at line 23 of file Sidebar.php.

◆ $helperData

$helperData
protected

Definition at line 28 of file Sidebar.php.

◆ $resolver

$resolver
protected

Definition at line 33 of file Sidebar.php.

◆ $summaryQty

$summaryQty
protected

Definition at line 38 of file Sidebar.php.


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