Totals.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Block\Cart;
  7. use Magento\Framework\View\Element\BlockInterface;
  8. use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Totals extends \Magento\Checkout\Block\Cart\AbstractCart
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $_totalRenderers;
  19. /**
  20. * @var string
  21. */
  22. protected $_defaultRenderer = \Magento\Checkout\Block\Total\DefaultTotal::class;
  23. /**
  24. * @var array
  25. */
  26. protected $_totals = null;
  27. /**
  28. * @var \Magento\Sales\Model\Config
  29. */
  30. protected $_salesConfig;
  31. /**
  32. * @var LayoutProcessorInterface[]
  33. */
  34. protected $layoutProcessors;
  35. /**
  36. * @param \Magento\Framework\View\Element\Template\Context $context
  37. * @param \Magento\Customer\Model\Session $customerSession
  38. * @param \Magento\Checkout\Model\Session $checkoutSession
  39. * @param \Magento\Sales\Model\Config $salesConfig
  40. * @param array $layoutProcessors
  41. * @param array $data
  42. * @codeCoverageIgnore
  43. */
  44. public function __construct(
  45. \Magento\Framework\View\Element\Template\Context $context,
  46. \Magento\Customer\Model\Session $customerSession,
  47. \Magento\Checkout\Model\Session $checkoutSession,
  48. \Magento\Sales\Model\Config $salesConfig,
  49. array $layoutProcessors = [],
  50. array $data = []
  51. ) {
  52. $this->_salesConfig = $salesConfig;
  53. parent::__construct($context, $customerSession, $checkoutSession, $data);
  54. $this->_isScopePrivate = true;
  55. $this->layoutProcessors = $layoutProcessors;
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getJsLayout()
  61. {
  62. foreach ($this->layoutProcessors as $processor) {
  63. $this->jsLayout = $processor->process($this->jsLayout);
  64. }
  65. return json_encode($this->jsLayout, JSON_HEX_TAG);
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function getTotals()
  71. {
  72. if ($this->_totals === null) {
  73. return parent::getTotals();
  74. }
  75. return $this->_totals;
  76. }
  77. /**
  78. * @param array $value
  79. * @return $this
  80. * @codeCoverageIgnore
  81. */
  82. public function setTotals($value)
  83. {
  84. $this->_totals = $value;
  85. return $this;
  86. }
  87. /**
  88. * @param string $code
  89. * @return BlockInterface
  90. */
  91. protected function _getTotalRenderer($code)
  92. {
  93. $blockName = $code . '_total_renderer';
  94. $block = $this->getLayout()->getBlock($blockName);
  95. if (!$block) {
  96. $renderer = $this->_salesConfig->getTotalsRenderer('quote', 'totals', $code);
  97. if (!empty($renderer)) {
  98. $block = $renderer;
  99. } else {
  100. $block = $this->_defaultRenderer;
  101. }
  102. $block = $this->getLayout()->createBlock($block, $blockName);
  103. }
  104. /**
  105. * Transfer totals to renderer
  106. */
  107. $block->setTotals($this->getTotals());
  108. return $block;
  109. }
  110. /**
  111. * @param mixed $total
  112. * @param int|null $area
  113. * @param int $colspan
  114. * @return string
  115. */
  116. public function renderTotal($total, $area = null, $colspan = 1)
  117. {
  118. $code = $total->getCode();
  119. if ($total->getAs()) {
  120. $code = $total->getAs();
  121. }
  122. return $this->_getTotalRenderer(
  123. $code
  124. )->setTotal(
  125. $total
  126. )->setColspan(
  127. $colspan
  128. )->setRenderingArea(
  129. $area === null ? -1 : $area
  130. )->toHtml();
  131. }
  132. /**
  133. * Render totals html for specific totals area (footer, body)
  134. *
  135. * @param null|string $area
  136. * @param int $colspan
  137. * @return string
  138. */
  139. public function renderTotals($area = null, $colspan = 1)
  140. {
  141. $html = '';
  142. foreach ($this->getTotals() as $total) {
  143. if ($total->getArea() != $area && $area != -1) {
  144. continue;
  145. }
  146. $html .= $this->renderTotal($total, $area, $colspan);
  147. }
  148. return $html;
  149. }
  150. /**
  151. * Check if we have display grand total in base currency
  152. *
  153. * @return bool
  154. */
  155. public function needDisplayBaseGrandtotal()
  156. {
  157. $quote = $this->getQuote();
  158. if ($quote->getBaseCurrencyCode() != $quote->getQuoteCurrencyCode()) {
  159. return true;
  160. }
  161. return false;
  162. }
  163. /**
  164. * Get formated in base currency base grand total value
  165. *
  166. * @return string
  167. */
  168. public function displayBaseGrandtotal()
  169. {
  170. $firstTotal = reset($this->_totals);
  171. if ($firstTotal) {
  172. $total = $firstTotal->getAddress()->getBaseGrandTotal();
  173. return $this->_storeManager->getStore()->getBaseCurrency()->format($total, [], true);
  174. }
  175. return '-';
  176. }
  177. /**
  178. * Get active or custom quote
  179. *
  180. * @return \Magento\Quote\Model\Quote
  181. */
  182. public function getQuote()
  183. {
  184. if ($this->getCustomQuote()) {
  185. return $this->getCustomQuote();
  186. }
  187. if (null === $this->_quote) {
  188. $this->_quote = $this->_checkoutSession->getQuote();
  189. }
  190. return $this->_quote;
  191. }
  192. }