Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GrandTotalDetailsPlugin.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Quote\Api\Data\TotalSegmentExtensionFactory;
11 
13 {
17  private $detailsFactory;
18 
22  private $ratesFactory;
23 
27  private $totalSegmentExtensionFactory;
28 
32  private $taxConfig;
33 
37  private $code;
38 
42  private $serializer;
43 
53  public function __construct(
54  \Magento\Tax\Api\Data\GrandTotalDetailsInterfaceFactory $detailsFactory,
55  \Magento\Tax\Api\Data\GrandTotalRatesInterfaceFactory $ratesFactory,
56  TotalSegmentExtensionFactory $totalSegmentExtensionFactory,
57  \Magento\Tax\Model\Config $taxConfig,
58  Json $serializer
59  ) {
60  $this->detailsFactory = $detailsFactory;
61  $this->ratesFactory = $ratesFactory;
62  $this->totalSegmentExtensionFactory = $totalSegmentExtensionFactory;
63  $this->taxConfig = $taxConfig;
64  $this->serializer = $serializer;
65  $this->code = 'tax';
66  }
67 
72  protected function getRatesData($rates)
73  {
74  $taxRates = [];
75  foreach ($rates as $rate) {
76  $taxRate = $this->ratesFactory->create([]);
77  $taxRate->setPercent($rate['percent']);
78  $taxRate->setTitle($rate['title']);
79  $taxRates[] = $taxRate;
80  }
81  return $taxRates;
82  }
83 
92  public function afterProcess(
93  \Magento\Quote\Model\Cart\TotalsConverter $subject,
94  array $totalSegments,
95  array $addressTotals = []
96  ) {
97 
98  if (!array_key_exists($this->code, $addressTotals)) {
99  return $totalSegments;
100  }
101 
102  $taxes = $addressTotals['tax']->getData();
103  if (!array_key_exists('full_info', $taxes)) {
104  return $totalSegments;
105  }
106 
107  $detailsId = 1;
108  $finalData = [];
109  $fullInfo = $taxes['full_info'];
110  if (is_string($fullInfo)) {
111  $fullInfo = $this->serializer->unserialize($fullInfo);
112  }
113  foreach ($fullInfo as $info) {
114  if ((array_key_exists('hidden', $info) && $info['hidden'])
115  || ($info['amount'] == 0 && $this->taxConfig->displayCartZeroTax())
116  ) {
117  continue;
118  }
119 
120  $taxDetails = $this->detailsFactory->create([]);
121  $taxDetails->setAmount($info['amount']);
122  $taxRates = $this->getRatesData($info['rates']);
123  $taxDetails->setRates($taxRates);
124  $taxDetails->setGroupId($detailsId);
125  $finalData[] = $taxDetails;
126  $detailsId++;
127  }
128  $attributes = $totalSegments[$this->code]->getExtensionAttributes();
129  if ($attributes === null) {
130  $attributes = $this->totalSegmentExtensionFactory->create();
131  }
132  $attributes->setTaxGrandtotalDetails($finalData);
133  $totalSegments[$this->code]->setExtensionAttributes($attributes);
134  return $totalSegments;
135  }
136 }
$rates
Definition: tax.phtml:35
afterProcess(\Magento\Quote\Model\Cart\TotalsConverter $subject, array $totalSegments, array $addressTotals=[])
$attributes
Definition: matrix.phtml:13
__construct(\Magento\Tax\Api\Data\GrandTotalDetailsInterfaceFactory $detailsFactory, \Magento\Tax\Api\Data\GrandTotalRatesInterfaceFactory $ratesFactory, TotalSegmentExtensionFactory $totalSegmentExtensionFactory, \Magento\Tax\Model\Config $taxConfig, Json $serializer)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$taxRate
Definition: tax_rule.php:12