Cart.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Plugin\Checkout\CustomerData;
  7. class Cart
  8. {
  9. /**
  10. * @var \Magento\Customer\Model\Session
  11. */
  12. protected $checkoutSession;
  13. /**
  14. * @var \Magento\Checkout\Helper\Data
  15. */
  16. protected $checkoutHelper;
  17. /**
  18. * @var \Magento\Tax\Block\Item\Price\Renderer
  19. */
  20. protected $itemPriceRenderer;
  21. /**
  22. * @var \Magento\Quote\Model\Quote|null
  23. */
  24. protected $quote = null;
  25. /**
  26. * @var array|null
  27. */
  28. protected $totals = null;
  29. /**
  30. * @param \Magento\Checkout\Model\Session $checkoutSession
  31. * @param \Magento\Checkout\Helper\Data $checkoutHelper
  32. * @param \Magento\Tax\Block\Item\Price\Renderer $itemPriceRenderer
  33. */
  34. public function __construct(
  35. \Magento\Checkout\Model\Session $checkoutSession,
  36. \Magento\Checkout\Helper\Data $checkoutHelper,
  37. \Magento\Tax\Block\Item\Price\Renderer $itemPriceRenderer
  38. ) {
  39. $this->checkoutSession = $checkoutSession;
  40. $this->checkoutHelper = $checkoutHelper;
  41. $this->itemPriceRenderer = $itemPriceRenderer;
  42. }
  43. /**
  44. * Add tax data to result
  45. *
  46. * @param \Magento\Checkout\CustomerData\Cart $subject
  47. * @param array $result
  48. * @return array
  49. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  50. */
  51. public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
  52. {
  53. $result['subtotal_incl_tax'] = $this->checkoutHelper->formatPrice($this->getSubtotalInclTax());
  54. $result['subtotal_excl_tax'] = $this->checkoutHelper->formatPrice($this->getSubtotalExclTax());
  55. $items =$this->getQuote()->getAllVisibleItems();
  56. if (is_array($result['items'])) {
  57. foreach ($result['items'] as $key => $itemAsArray) {
  58. if ($item = $this->findItemById($itemAsArray['item_id'], $items)) {
  59. $this->itemPriceRenderer->setItem($item);
  60. $this->itemPriceRenderer->setTemplate('checkout/cart/item/price/sidebar.phtml');
  61. $result['items'][$key]['product_price']=$this->itemPriceRenderer->toHtml();
  62. }
  63. }
  64. }
  65. return $result;
  66. }
  67. /**
  68. * Get subtotal, including tax
  69. *
  70. * @return float
  71. */
  72. protected function getSubtotalInclTax()
  73. {
  74. $subtotal = 0;
  75. $totals = $this->getTotals();
  76. if (isset($totals['subtotal'])) {
  77. $subtotal = $totals['subtotal']->getValueInclTax() ?: $totals['subtotal']->getValue();
  78. }
  79. return $subtotal;
  80. }
  81. /**
  82. * Get subtotal, excluding tax
  83. *
  84. * @return float
  85. */
  86. protected function getSubtotalExclTax()
  87. {
  88. $subtotal = 0;
  89. $totals = $this->getTotals();
  90. if (isset($totals['subtotal'])) {
  91. $subtotal = $totals['subtotal']->getValueExclTax() ?: $totals['subtotal']->getValue();
  92. }
  93. return $subtotal;
  94. }
  95. /**
  96. * Get totals
  97. *
  98. * @return array
  99. */
  100. public function getTotals()
  101. {
  102. // TODO: TODO: MAGETWO-34824 duplicate \Magento\Checkout\CustomerData\Cart::getSectionData
  103. if (empty($this->totals)) {
  104. $this->totals = $this->getQuote()->getTotals();
  105. }
  106. return $this->totals;
  107. }
  108. /**
  109. * Get active quote
  110. *
  111. * @return \Magento\Quote\Model\Quote
  112. */
  113. protected function getQuote()
  114. {
  115. if (null === $this->quote) {
  116. $this->quote = $this->checkoutSession->getQuote();
  117. }
  118. return $this->quote;
  119. }
  120. /**
  121. * Find item by id in items haystack
  122. *
  123. * @param int $id
  124. * @param array $itemsHaystack
  125. * @return \Magento\Quote\Model\Quote\Item | bool
  126. */
  127. protected function findItemById($id, $itemsHaystack)
  128. {
  129. if (is_array($itemsHaystack)) {
  130. foreach ($itemsHaystack as $item) {
  131. /** @var $item \Magento\Quote\Model\Quote\Item */
  132. if ((int)$item->getItemId() == $id) {
  133. return $item;
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. }