Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Totals.php
Go to the documentation of this file.
1 <?php
7 
10 
15 class Totals extends \Magento\Checkout\Block\Cart\AbstractCart
16 {
20  protected $_totalRenderers;
21 
25  protected $_defaultRenderer = \Magento\Checkout\Block\Total\DefaultTotal::class;
26 
30  protected $_totals = null;
31 
35  protected $_salesConfig;
36 
40  protected $layoutProcessors;
41 
51  public function __construct(
52  \Magento\Framework\View\Element\Template\Context $context,
53  \Magento\Customer\Model\Session $customerSession,
54  \Magento\Checkout\Model\Session $checkoutSession,
55  \Magento\Sales\Model\Config $salesConfig,
56  array $layoutProcessors = [],
57  array $data = []
58  ) {
59  $this->_salesConfig = $salesConfig;
60  parent::__construct($context, $customerSession, $checkoutSession, $data);
61  $this->_isScopePrivate = true;
62  $this->layoutProcessors = $layoutProcessors;
63  }
64 
68  public function getJsLayout()
69  {
70  foreach ($this->layoutProcessors as $processor) {
71  $this->jsLayout = $processor->process($this->jsLayout);
72  }
73 
74  return json_encode($this->jsLayout, JSON_HEX_TAG);
75  }
76 
80  public function getTotals()
81  {
82  if ($this->_totals === null) {
83  return parent::getTotals();
84  }
85  return $this->_totals;
86  }
87 
93  public function setTotals($value)
94  {
95  $this->_totals = $value;
96  return $this;
97  }
98 
103  protected function _getTotalRenderer($code)
104  {
105  $blockName = $code . '_total_renderer';
106  $block = $this->getLayout()->getBlock($blockName);
107  if (!$block) {
108  $renderer = $this->_salesConfig->getTotalsRenderer('quote', 'totals', $code);
109  if (!empty($renderer)) {
110  $block = $renderer;
111  } else {
113  }
114 
115  $block = $this->getLayout()->createBlock($block, $blockName);
116  }
120  $block->setTotals($this->getTotals());
121  return $block;
122  }
123 
130  public function renderTotal($total, $area = null, $colspan = 1)
131  {
132  $code = $total->getCode();
133  if ($total->getAs()) {
134  $code = $total->getAs();
135  }
136  return $this->_getTotalRenderer(
137  $code
138  )->setTotal(
139  $total
140  )->setColspan(
141  $colspan
142  )->setRenderingArea(
143  $area === null ? -1 : $area
144  )->toHtml();
145  }
146 
154  public function renderTotals($area = null, $colspan = 1)
155  {
156  $html = '';
157  foreach ($this->getTotals() as $total) {
158  if ($total->getArea() != $area && $area != -1) {
159  continue;
160  }
161  $html .= $this->renderTotal($total, $area, $colspan);
162  }
163  return $html;
164  }
165 
171  public function needDisplayBaseGrandtotal()
172  {
173  $quote = $this->getQuote();
174  if ($quote->getBaseCurrencyCode() != $quote->getQuoteCurrencyCode()) {
175  return true;
176  }
177  return false;
178  }
179 
185  public function displayBaseGrandtotal()
186  {
187  $firstTotal = reset($this->_totals);
188  if ($firstTotal) {
189  $total = $firstTotal->getAddress()->getBaseGrandTotal();
190  return $this->_storeManager->getStore()->getBaseCurrency()->format($total, [], true);
191  }
192  return '-';
193  }
194 
200  public function getQuote()
201  {
202  if ($this->getCustomQuote()) {
203  return $this->getCustomQuote();
204  }
205 
206  if (null === $this->_quote) {
207  $this->_quote = $this->_checkoutSession->getQuote();
208  }
209  return $this->_quote;
210  }
211 }
$processor
Definition: 404.php:10
$quote
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\Config $salesConfig, array $layoutProcessors=[], array $data=[])
Definition: Totals.php:51
$block
Definition: block.php:8
renderTotal($total, $area=null, $colspan=1)
Definition: Totals.php:130
renderTotals($area=null, $colspan=1)
Definition: Totals.php:154
$value
Definition: gender.phtml:16
$code
Definition: info.phtml:12