. * */ namespace Vvveb\System\Cart; trait TaxTrait { public function addTax($name, $price, $taxTypeId) { if ($price && $taxTypeId) { $this->taxes[$name] = ['price' => $price, 'tax_type_id' => $taxTypeId]; } } public function removeTax($name) { unset($this->taxes[$name]); } public function getTaxes() { $taxes = []; $this->total_tax = 0; $products = $this->products + $this->taxes; foreach ($products as $product) { if (isset($product['tax_type_id']) && $product['tax_type_id']) { $tax_rates = $this->tax->getRates($product['price'], $product['tax_type_id']); foreach ($tax_rates as $tax_rate) { $rateId = $tax_rate['tax_rate_id'] ?? false; if (! isset($taxes[$rateId])) { $taxes[$rateId] = $tax_rate; $taxes[$rateId]['value'] = 0; } $taxes[$rateId]['value'] += ($tax_rate['amount'] * ($product['quantity'] ?? 1)); $this->total_tax = $taxes[$rateId]['value']; } } } return $taxes; } public function getTaxTotal() { return $this->total_tax; } function addTaxTotal() { $taxes = $this->getTaxes(); foreach ($taxes as $tax) { $this->addTotal('tax.' . $tax['tax_rate_id'], $tax['name'], $tax['value']); } } }