Sidebar.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model;
  7. use Magento\Checkout\Helper\Data as HelperData;
  8. use Magento\Checkout\Model\Cart;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Locale\ResolverInterface;
  11. use Magento\Quote\Api\Data\CartItemInterface;
  12. use Magento\Quote\Model\Quote\Address\Total;
  13. /**
  14. * @deprecated 100.1.0
  15. */
  16. class Sidebar
  17. {
  18. /**
  19. * @var Cart
  20. */
  21. protected $cart;
  22. /**
  23. * @var HelperData
  24. */
  25. protected $helperData;
  26. /**
  27. * @var ResolverInterface
  28. */
  29. protected $resolver;
  30. /**
  31. * @var int
  32. */
  33. protected $summaryQty;
  34. /**
  35. * @param Cart $cart
  36. * @param HelperData $helperData
  37. * @param ResolverInterface $resolver
  38. * @codeCoverageIgnore
  39. */
  40. public function __construct(
  41. Cart $cart,
  42. HelperData $helperData,
  43. ResolverInterface $resolver
  44. ) {
  45. $this->cart = $cart;
  46. $this->helperData = $helperData;
  47. $this->resolver = $resolver;
  48. }
  49. /**
  50. * Compile response data
  51. *
  52. * @param string $error
  53. * @return array
  54. */
  55. public function getResponseData($error = '')
  56. {
  57. if (empty($error)) {
  58. $response = [
  59. 'success' => true,
  60. ];
  61. } else {
  62. $response = [
  63. 'success' => false,
  64. 'error_message' => $error,
  65. ];
  66. }
  67. return $response;
  68. }
  69. /**
  70. * Check if required quote item exist
  71. *
  72. * @param int $itemId
  73. * @throws LocalizedException
  74. * @return $this
  75. */
  76. public function checkQuoteItem($itemId)
  77. {
  78. $item = $this->cart->getQuote()->getItemById($itemId);
  79. if (!$item instanceof CartItemInterface) {
  80. throw new LocalizedException(__("The quote item isn't found. Verify the item and try again."));
  81. }
  82. return $this;
  83. }
  84. /**
  85. * Remove quote item
  86. *
  87. * @param int $itemId
  88. * @return $this
  89. */
  90. public function removeQuoteItem($itemId)
  91. {
  92. $this->cart->removeItem($itemId);
  93. $this->cart->save();
  94. return $this;
  95. }
  96. /**
  97. * Update quote item
  98. *
  99. * @param int $itemId
  100. * @param int $itemQty
  101. * @throws LocalizedException
  102. * @return $this
  103. */
  104. public function updateQuoteItem($itemId, $itemQty)
  105. {
  106. $itemData = [$itemId => ['qty' => $this->normalize($itemQty)]];
  107. $this->cart->updateItems($itemData)->save();
  108. return $this;
  109. }
  110. /**
  111. * Apply normalization filter to item qty value
  112. *
  113. * @param int $itemQty
  114. * @return int|array
  115. */
  116. protected function normalize($itemQty)
  117. {
  118. if ($itemQty) {
  119. $filter = new \Zend_Filter_LocalizedToNormalized(
  120. ['locale' => $this->resolver->getLocale()]
  121. );
  122. return $filter->filter($itemQty);
  123. }
  124. return $itemQty;
  125. }
  126. /**
  127. * Retrieve summary qty
  128. *
  129. * @return int
  130. */
  131. protected function getSummaryQty()
  132. {
  133. if (!$this->summaryQty) {
  134. $this->summaryQty = $this->cart->getSummaryQty();
  135. }
  136. return $this->summaryQty;
  137. }
  138. /**
  139. * Retrieve summary qty text
  140. *
  141. * @return string
  142. */
  143. protected function getSummaryText()
  144. {
  145. return ($this->getSummaryQty() == 1) ? __(' item') : __(' items');
  146. }
  147. /**
  148. * Retrieve subtotal block html
  149. *
  150. * @return string
  151. */
  152. protected function getSubtotalHtml()
  153. {
  154. $totals = $this->cart->getQuote()->getTotals();
  155. $subtotal = isset($totals['subtotal']) && $totals['subtotal'] instanceof Total
  156. ? $totals['subtotal']->getValue()
  157. : 0;
  158. return $this->helperData->formatPrice($subtotal);
  159. }
  160. }