. * */ namespace Vvveb\System\Cart; trait TotalTrait { protected $totals = []; function addTotal($key, $title, $value, $text = '') { $data = ['key' => $key, 'title' => $title, 'value' => $value, 'value_formatted' => $this->currency->format($value), 'text' => $text]; $this->totals[$key] = $data; $this->write(); } function removeTotal($key) { unset($this->totals[$key]); } public function getSubTotal() { $total = 0; foreach ($this->products as $product) { $total += $product['total']; } return $total; } function getTotals() { //include taxes $this->addTaxTotal(); $this->addCouponTotal(); return $this->totals; } public function getGrandTotal() { $sum = 0; if ($this->totals) { foreach ($this->totals as $total) { $sum += (float)($total['value'] ?? 0); } } return $sum; } public function getGrandTotalFormatted() { $sum = $this->getGrandTotal(); return $this->currency->format($sum); } }