Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Fix cart weight to include quantity, moved total code to trait.

givanz104316515591
diff --git a/system/cart/cart.php b/system/cart/cart.php
index 4242cf6..0049b76 100755
--- a/system/cart/cart.php
+++ b/system/cart/cart.php
@@ -58,7 +58,7 @@ class Cart {
protected $total_items = 0;
- use TaxTrait, ProductOptionTrait, CouponTrait;
+ use TaxTrait, ProductOptionTrait, CouponTrait, TotalTrait;
public static function getInstance($options = []) {
static $inst = null;
@@ -336,60 +336,13 @@ class Cart {
foreach ($this->products as $product) {
if ($product['requires_shipping']) {
- $weight += $this->weight->convert($product['weight'], $product['weight_type_id'], $this->options['weight_type_id']);
+ $weight += $this->weight->convert($product['weight'], $product['weight_type_id'], $this->options['weight_type_id']) * $product['quantity'];
}
}
return $weight;
}
- public function getSubTotal() {
- $total = 0;
-
- foreach ($this->products as $product) {
- $total += $product['total'];
- }
-
- return $total;
- }
-
- public function getGrandTotal() {
- $sum = 0;
-
- if ($this->totals) {
- foreach ($this->totals as $total) {
- $sum += $total['value'] ?? 0;
- }
- }
-
- return $sum;
- }
-
- public function getGrandTotalFormatted() {
- $sum = $this->getGrandTotal();
-
- return $this->currency->format($sum);
- }
-
- 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]);
- }
-
- function getTotals() {
- //include taxes
- $this->addTaxTotal();
- $this->addCouponTotal();
-
- return $this->totals;
- }
-
public function countProducts() {
$product_total = 0;
diff --git a/system/cart/total-trait.php b/system/cart/total-trait.php
new file mode 100644
index 0000000..faa5ea4
--- /dev/null
+++ b/system/cart/total-trait.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * Vvveb
+ *
+ * Copyright (C) 2022 Ziadin Givan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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 += $total['value'] ?? 0;
+ }
+ }
+
+ return $sum;
+ }
+
+ public function getGrandTotalFormatted() {
+ $sum = $this->getGrandTotal();
+
+ return $this->currency->format($sum);
+ }
+}
diff --git a/system/setting.php b/system/setting.php
index 147daf3..2fcbd33 100755
--- a/system/setting.php
+++ b/system/setting.php
@@ -109,7 +109,7 @@ class Setting {
}
public function delete($namespace, $key, $site_id = SITE_ID) {
- if (! $namespace || ! $key) {
+ if (! $namespace) {
return;
}