Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Added namespace for totals
givanzd2e7a00ea966
diff --git a/app/controller/cart/cart.php b/app/controller/cart/cart.phpindex 474108c..1845b52 100755--- a/app/controller/cart/cart.php+++ b/app/controller/cart/cart.php@@ -72,7 +72,7 @@ class Cart extends Base {$cart = ['products' => $this->cart->getAll(),- 'totals' => $this->cart->getTotals(),+ 'totals' => $this->cart->getAllTotals(),'total_items' => $this->cart->getNoProducts(),'total_weight' => $this->cart->getWeight(),'total_price' => $this->cart->getNoProducts(),diff --git a/app/controller/checkout/checkout.php b/app/controller/checkout/checkout.phpindex 4ad971f..563b875 100755--- a/app/controller/checkout/checkout.php+++ b/app/controller/checkout/checkout.php@@ -270,7 +270,7 @@ class Checkout extends Base {if (($errors = $validator->validate($this->request->post)) === true) {$checkoutInfo['products'] = $this->cart->getAll();$checkoutInfo['product_options'] = $this->cart->getProductOptions();- $checkoutInfo['totals'] = $this->cart->getTotals();+ $checkoutInfo['totals'] = $this->cart->getAllTotals();$checkoutInfo['total'] = $grandTotal;$checkoutInfo += prefixArrayKeys('shipping_', $checkoutInfo['shipping_address']);$checkoutInfo += prefixArrayKeys('billing_', $checkoutInfo['billing_address']);diff --git a/app/template/cart/cart.tpl b/app/template/cart/cart.tplold mode 100755new mode 100644index e105447..0de175e--- a/app/template/cart/cart.tpl+++ b/app/template/cart/cart.tpl@@ -1,6 +1,9 @@import(common.tpl)-[data-v-cart-page] [data-v-cart-cart-*]|innerText = $this->cart['@@__data-v-cart-cart-(*)__@@']+[data-v-cart-page][data-v-cart-total_items] = $cart['total_items']+//[data-v-cart-page] [data-v-cart-total] = $cart['total']+[data-v-cart-page] [data-v-cart-total_formatted] = $cart['total_formatted']+//[data-v-cart-page] [data-v-cart-*]|innerText = $this->cart['@@__data-v-cart-(*)__@@']@cart-product = [data-v-cart] [data-v-cart-product]@@ -88,3 +91,4 @@ if(is_array($coupons)) foreach ($coupons as $index => $coupon) {@coupon input[data-v-cart-coupon-*]|value = $coupon['@@__data-v-cart-coupon-(*)__@@']@coupon|after = <?php }?>+diff --git a/app/template/components/cart.tpl b/app/template/components/cart.tplold mode 100755new mode 100644index bee8ae9..cc314d6--- a/app/template/components/cart.tpl+++ b/app/template/components/cart.tpl@@ -14,10 +14,11 @@ $_pagination_limit = isset($cart['limit']) ? $cart['limit'] : 5;$products = $cart['products'] ?? [];?>-[data-v-component-cart] [data-v-total_items] = $cart['total_items']-[data-v-component-cart] [data-v-grand-total] = $cart['total']-[data-v-component-cart] [data-v-grand-total_formatted] = $cart['total_formatted']-[data-v-component-cart] [data-v-cart-cart-*]|innerText = $cart['@@__data-v-cart-cart-(*)__@@']+[data-v-component-cart] [data-v-cart-total_items] = $cart['total_items']+//[data-v-component-cart] [data-v-cart-total] = $cart['total']+[data-v-component-cart] [data-v-cart-total_formatted] = $cart['total_formatted']++@cart-product|before = <?php$_default = (isset($vvveb_is_page_edit) && $vvveb_is_page_edit ) ? [0 => ['product_id' => 1, 'image' => '#']] : false;@@ -104,3 +105,5 @@ if(is_array($coupons)) foreach ($coupons as $index => $coupon) {@coupon input[data-v-cart-coupon-*]|value = $coupon['@@__data-v-cart-coupon-(*)__@@']@coupon|after = <?php }?>++[data-v-component-cart] [data-v-cart-*]|innerText = $cart['@@__data-v-cart-(*)__@@']diff --git a/system/cart/coupon-trait.php b/system/cart/coupon-trait.phpindex af49159..56d8e88 100644--- a/system/cart/coupon-trait.php+++ b/system/cart/coupon-trait.php@@ -30,7 +30,7 @@ trait CouponTrait {public function addCoupon($code) {if ($code) {$coupon = new CouponSQL();- $options = $this->options + ['code' => $code, 'status' => 1];+ $options = ['code' => $code, 'status' => 1] + $this->options;$result = $coupon->get($options);if ($result && $result['code'] == $code) {@@ -45,7 +45,7 @@ trait CouponTrait {public function removeCoupon($code) {$coupon = $this->coupons[$code];- $this->removeTotal('coupon.' . $coupon['coupon_id']);+ $this->removeTotal('coupon', 'coupon.' . $coupon['coupon_id']);unset($this->coupons[$code]);return true;@@ -58,14 +58,20 @@ trait CouponTrait {function addCouponTotal() {$coupons = $this->getCoupons();+ $total = $this->getSubTotal();+ //include taxes in discount calculation+ $taxes = $this->getTotals('tax');+ foreach ($taxes as $tax) {+ $total += $tax['value'];+ }+foreach ($coupons as $coupon) {if ($coupon['type'] == 'P') {- $discount = (($coupon['discount'] * $this->getSubTotal()) / 100);+ $discount = (($coupon['discount'] * $total) / 100);} else {$discount = $coupon['discount'];}-- $this->addTotal('coupon.' . $coupon['coupon_id'], $coupon['name'], $discount);+ $this->addTotal('coupon', 'coupon.' . $coupon['coupon_id'], $coupon['name'], -$discount);}}}diff --git a/system/cart/product-option-trait.php b/system/cart/product-option-trait.phpindex a78da30..092ca44 100644--- a/system/cart/product-option-trait.php+++ b/system/cart/product-option-trait.php@@ -52,7 +52,7 @@ trait ProductOptionTrait {$product_options = $this->getProductOptions();foreach ($product_options as $product_option) {- $this->addTotal('product_option.' . $product_option['product_option_rate_id'], $product_option['name'], $product_option['value']);+ $this->addTotal('product_option', 'product_option.' . $product_option['product_option_rate_id'], $product_option['name'], $product_option['value']);}}}diff --git a/system/cart/tax-trait.php b/system/cart/tax-trait.phpindex 841a159..2158f0b 100644--- a/system/cart/tax-trait.php+++ b/system/cart/tax-trait.php@@ -65,9 +65,10 @@ trait TaxTrait {function addTaxTotal() {$taxes = $this->getTaxes();-+ $this->removeTotal('tax');+foreach ($taxes as $tax) {- $this->addTotal('tax.' . $tax['tax_rate_id'], $tax['name'], $tax['value']);+ $this->addTotal('tax', 'tax.' . $tax['tax_rate_id'], $tax['name'], $tax['value']);}}}diff --git a/system/cart/total-trait.php b/system/cart/total-trait.phpindex 99dd352..aa5424f 100644--- a/system/cart/total-trait.php+++ b/system/cart/total-trait.php@@ -25,15 +25,21 @@ 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];+ function addTotal($namespace, $key, $title, $value, $text = '') {+ $data = ['namespace' => $namespace, '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]);+ function removeTotal($namespace, $key = false) {+ if ($key) {+ unset($this->totals[$key]);+ } else {+ foreach ($this->totals as $key => $total) {+ if ($total['namespace'] == $namespace) unset($this->totals[$key]);+ }+ }}public function getSubTotal() {@@ -46,7 +52,22 @@ trait TotalTrait {return $total;}- function getTotals() {+ function getTotals($namespace = false, $key = false) {+ if ($key) {+ return $this->totals[$key];+ } else if ($namespace) {+ $totals = [];+ foreach ($this->totals as $key => $total) {+ if ($total['namespace'] == $namespace) $totals[$key] = $total;+ }++ return $totals;+ }++ return $this->totals;+ }++ function getAllTotals() {//include taxes$this->addTaxTotal();$this->addCouponTotal();