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 
9 
15 {
24  protected $_totals;
25 
29  protected $_order = null;
30 
36  protected $_coreRegistry = null;
37 
43  public function __construct(
44  \Magento\Framework\View\Element\Template\Context $context,
45  \Magento\Framework\Registry $registry,
46  array $data = []
47  ) {
48  $this->_coreRegistry = $registry;
49  parent::__construct($context, $data);
50  }
51 
57  protected function _beforeToHtml()
58  {
59  $this->_initTotals();
60  foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $child) {
61  if (method_exists($child, 'initTotals') && is_callable([$child, 'initTotals'])) {
62  $child->initTotals();
63  }
64  }
65  return parent::_beforeToHtml();
66  }
67 
73  public function getOrder()
74  {
75  if ($this->_order === null) {
76  if ($this->hasData('order')) {
77  $this->_order = $this->_getData('order');
78  } elseif ($this->_coreRegistry->registry('current_order')) {
79  $this->_order = $this->_coreRegistry->registry('current_order');
80  } elseif ($this->getParentBlock()->getOrder()) {
81  $this->_order = $this->getParentBlock()->getOrder();
82  }
83  }
84  return $this->_order;
85  }
86 
91  public function setOrder($order)
92  {
93  $this->_order = $order;
94  return $this;
95  }
96 
102  public function getSource()
103  {
104  return $this->getOrder();
105  }
106 
112  protected function _initTotals()
113  {
114  $source = $this->getSource();
115 
116  $this->_totals = [];
117  $this->_totals['subtotal'] = new \Magento\Framework\DataObject(
118  ['code' => 'subtotal', 'value' => $source->getSubtotal(), 'label' => __('Subtotal')]
119  );
120 
124  if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
125  $this->_totals['shipping'] = new \Magento\Framework\DataObject(
126  [
127  'code' => 'shipping',
128  'field' => 'shipping_amount',
129  'value' => $this->getSource()->getShippingAmount(),
130  'label' => __('Shipping & Handling'),
131  ]
132  );
133  }
134 
138  if ((double)$this->getSource()->getDiscountAmount() != 0) {
139  if ($this->getSource()->getDiscountDescription()) {
140  $discountLabel = __('Discount (%1)', $source->getDiscountDescription());
141  } else {
142  $discountLabel = __('Discount');
143  }
144  $this->_totals['discount'] = new \Magento\Framework\DataObject(
145  [
146  'code' => 'discount',
147  'field' => 'discount_amount',
148  'value' => $source->getDiscountAmount(),
149  'label' => $discountLabel,
150  ]
151  );
152  }
153 
154  $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
155  [
156  'code' => 'grand_total',
157  'field' => 'grand_total',
158  'strong' => true,
159  'value' => $source->getGrandTotal(),
160  'label' => __('Grand Total'),
161  ]
162  );
163 
167  if ($this->getOrder()->isCurrencyDifferent()) {
168  $this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
169  [
170  'code' => 'base_grandtotal',
171  'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
172  'label' => __('Grand Total to be Charged'),
173  'is_formated' => true,
174  ]
175  );
176  }
177  return $this;
178  }
179 
187  public function addTotal(\Magento\Framework\DataObject $total, $after = null)
188  {
189  if ($after !== null && $after != 'last' && $after != 'first') {
190  $totals = [];
191  $added = false;
192  foreach ($this->_totals as $code => $item) {
193  $totals[$code] = $item;
194  if ($code == $after) {
195  $added = true;
196  $totals[$total->getCode()] = $total;
197  }
198  }
199  if (!$added) {
200  $last = array_pop($totals);
201  $totals[$total->getCode()] = $total;
202  $totals[$last->getCode()] = $last;
203  }
204  $this->_totals = $totals;
205  } elseif ($after == 'last') {
206  $this->_totals[$total->getCode()] = $total;
207  } elseif ($after == 'first') {
208  $totals = [$total->getCode() => $total];
209  $this->_totals = array_merge($totals, $this->_totals);
210  } else {
211  $last = array_pop($this->_totals);
212  $this->_totals[$total->getCode()] = $total;
213  $this->_totals[$last->getCode()] = $last;
214  }
215  return $this;
216  }
217 
225  public function addTotalBefore(\Magento\Framework\DataObject $total, $before = null)
226  {
227  if ($before !== null) {
228  if (!is_array($before)) {
229  $before = [$before];
230  }
231  foreach ($before as $beforeTotals) {
232  if (isset($this->_totals[$beforeTotals])) {
233  $totals = [];
234  foreach ($this->_totals as $code => $item) {
235  if ($code == $beforeTotals) {
236  $totals[$total->getCode()] = $total;
237  }
238  $totals[$code] = $item;
239  }
240  $this->_totals = $totals;
241  return $this;
242  }
243  }
244  }
245  $totals = [];
246  $first = array_shift($this->_totals);
247  $totals[$first->getCode()] = $first;
248  $totals[$total->getCode()] = $total;
249  foreach ($this->_totals as $code => $item) {
250  $totals[$code] = $item;
251  }
252  $this->_totals = $totals;
253  return $this;
254  }
255 
262  public function getTotal($code)
263  {
264  if (isset($this->_totals[$code])) {
265  return $this->_totals[$code];
266  }
267  return false;
268  }
269 
276  public function removeTotal($code)
277  {
278  unset($this->_totals[$code]);
279  return $this;
280  }
281 
294  public function applySortOrder($order)
295  {
296  \uksort(
297  $this->_totals,
298  function ($code1, $code2) use ($order) {
299  return ($order[$code1] ?? 0) <=> ($order[$code2] ?? 0);
300  }
301  );
302  return $this;
303  }
304 
311  public function getTotals($area = null)
312  {
313  $totals = [];
314  if ($area === null) {
316  } else {
317  $area = (string)$area;
318  foreach ($this->_totals as $total) {
319  $totalArea = (string)$total->getArea();
320  if ($totalArea == $area) {
321  $totals[] = $total;
322  }
323  }
324  }
325  return $totals;
326  }
327 
334  public function formatValue($total)
335  {
336  if (!$total->getIsFormated()) {
337  return $this->getOrder()->formatPrice($total->getValue());
338  }
339  return $total->getValue();
340  }
341 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
$order
Definition: order.php:55
__()
Definition: __.php:13
$totals
Definition: totalbar.phtml:10
addTotal(\Magento\Framework\DataObject $total, $after=null)
Definition: Totals.php:187
addTotalBefore(\Magento\Framework\DataObject $total, $before=null)
Definition: Totals.php:225
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, array $data=[])
Definition: Totals.php:43
$code
Definition: info.phtml:12