Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TotalsReader.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
16  protected $totalFactory;
17 
21  protected $collectorList;
22 
27  public function __construct(
30  ) {
31  $this->totalFactory = $totalFactory;
32  $this->collectorList = $collectorList;
33  }
34 
40  public function fetch(\Magento\Quote\Model\Quote $quote, array $total)
41  {
42  $output = [];
43  $total = $this->totalFactory->create()->setData($total);
45  foreach ($this->collectorList->getCollectors($quote->getStoreId()) as $reader) {
46  $data = $reader->fetch($quote, $total);
47  if ($data === null || empty($data)) {
48  continue;
49  }
50 
51  $totalInstance = $this->convert($data);
52  if (is_array($totalInstance)) {
53  foreach ($totalInstance as $item) {
54  $output = $this->merge($item, $output);
55  }
56  } else {
57  $output = $this->merge($totalInstance, $output);
58  }
59  }
60  return $output;
61  }
62 
67  protected function convert($total)
68  {
69  if ($total instanceof Total) {
70  return $total;
71  }
72 
73  if (count(array_column($total, 'code')) > 0) {
74  $totals = [];
75  foreach ($total as $item) {
76  $totals[] = $this->totalFactory->create()->setData($item);
77  }
78  return $totals;
79  }
80 
81  return $this->totalFactory->create()->setData($total);
82  }
83 
89  protected function merge(Total $totalInstance, $output)
90  {
91  if (array_key_exists($totalInstance->getCode(), $output)) {
92  $output[$totalInstance->getCode()] = $output[$totalInstance->getCode()]->addData(
93  $totalInstance->getData()
94  );
95  } else {
96  $output[$totalInstance->getCode()] = $totalInstance;
97  }
98  return $output;
99  }
100 }
merge(Total $totalInstance, $output)
getData($key='', $index=null)
Definition: DataObject.php:119
$quote
__construct(\Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory, \Magento\Quote\Model\Quote\TotalsCollectorList $collectorList)
$totals
Definition: totalbar.phtml:10