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
Calculate products weight on cart page
givanzd69361f6e591
diff --git a/admin/sql/mysqli/length_type.sql b/admin/sql/mysqli/length_type.sqlindex a3558b8..31146de 100755--- a/admin/sql/mysqli/length_type.sql+++ b/admin/sql/mysqli/length_type.sql@@ -11,7 +11,7 @@)BEGIN-- length_type- SELECT *+ SELECT *, length_type.length_type_id as array_keyFROM length_type AS length_typeINNER JOIN length_type_content ON length_type_content.length_type_id = length_type.length_type_idWHERE 1 = 1@@ -58,7 +58,7 @@PROCEDURE add(IN length_type ARRAY,IN language_id INT,- OUT insert_id+ OUT insert_id,OUT affected_rowsOUT insert_id)diff --git a/admin/sql/mysqli/weight_type.sql b/admin/sql/mysqli/weight_type.sqlindex 9607c08..1c3f72a 100755--- a/admin/sql/mysqli/weight_type.sql+++ b/admin/sql/mysqli/weight_type.sql@@ -11,7 +11,7 @@)BEGIN-- weight_type- SELECT *+ SELECT *, weight_type.weight_type_id as array_keyFROM weight_type AS weight_typeINNER JOIN weight_type_content ON weight_type_content.weight_type_id = weight_type.weight_type_idWHERE 1 = 1@@ -58,7 +58,7 @@PROCEDURE add(IN weight_type ARRAY,IN language_id INT,- OUT insert_id+ OUT insert_id,OUT insert_id)BEGINdiff --git a/admin/sql/pgsql/length_type.sql b/admin/sql/pgsql/length_type.sqlindex 876a414..d569779 100755--- a/admin/sql/pgsql/length_type.sql+++ b/admin/sql/pgsql/length_type.sql@@ -11,7 +11,7 @@)BEGIN-- length_type- SELECT *+ SELECT *, length_type.length_type_id as array_keyFROM length_type AS length_typeINNER JOIN length_type_content ON length_type_content.length_type_id = length_type.length_type_idWHERE 1 = 1@@ -58,7 +58,7 @@PROCEDURE add(IN length_type ARRAY,IN language_id INT,- OUT fetch_one+ OUT fetch_one,OUT insert_id)BEGINdiff --git a/admin/sql/pgsql/weight_type.sql b/admin/sql/pgsql/weight_type.sqlindex 74a8ab3..8a6efc6 100755--- a/admin/sql/pgsql/weight_type.sql+++ b/admin/sql/pgsql/weight_type.sql@@ -11,7 +11,7 @@)BEGIN-- weight_type- SELECT *+ SELECT *, weight_type.weight_type_id as array_keyFROM weight_type AS weight_typeINNER JOIN weight_type_content ON weight_type_content.weight_type_id = weight_type.weight_type_idWHERE 1 = 1@@ -58,7 +58,7 @@PROCEDURE add(IN weight_type ARRAY,IN language_id INT,- OUT fetch_one+ OUT fetch_one,OUT insert_id)BEGINdiff --git a/admin/sql/sqlite/length_type.sql b/admin/sql/sqlite/length_type.sqlindex a3558b8..31146de 100755--- a/admin/sql/sqlite/length_type.sql+++ b/admin/sql/sqlite/length_type.sql@@ -11,7 +11,7 @@)BEGIN-- length_type- SELECT *+ SELECT *, length_type.length_type_id as array_keyFROM length_type AS length_typeINNER JOIN length_type_content ON length_type_content.length_type_id = length_type.length_type_idWHERE 1 = 1@@ -58,7 +58,7 @@PROCEDURE add(IN length_type ARRAY,IN language_id INT,- OUT insert_id+ OUT insert_id,OUT affected_rowsOUT insert_id)diff --git a/admin/sql/sqlite/weight_type.sql b/admin/sql/sqlite/weight_type.sqlindex 0b3af25..31a2b14 100755--- a/admin/sql/sqlite/weight_type.sql+++ b/admin/sql/sqlite/weight_type.sql@@ -11,7 +11,7 @@)BEGIN-- weight_type- SELECT *+ SELECT *, weight_type.weight_type_id as array_keyFROM weight_type AS weight_typeINNER JOIN weight_type_content ON weight_type_content.weight_type_id = weight_type.weight_type_idWHERE 1 = 1@@ -58,7 +58,7 @@PROCEDURE add(IN weight_type ARRAY,IN language_id INT,- OUT insert_id+ OUT insert_id,OUT insert_id)BEGINdiff --git a/app/component/breadcrumb.php b/app/component/breadcrumb.phpindex 944e713..1c0cf14 100755--- a/app/component/breadcrumb.php+++ b/app/component/breadcrumb.php@@ -158,6 +158,14 @@ class Breadcrumb extends ComponentBase {['text' => $slug, 'url' => false],];+ break;+ //compare+ case 'cart/cart/index':+ $breadcrumb = [+ ['text' => $homeText, 'url' => '/'],+ ['text' => __('Cart'), 'url' => false],+ ];+break;//comparecase 'cart/compare/index':diff --git a/app/controller/cart/cart.php b/app/controller/cart/cart.phpindex 86d84e6..ffc66d7 100755--- a/app/controller/cart/cart.php+++ b/app/controller/cart/cart.php@@ -32,12 +32,15 @@ class Cart extends Base {use CouponTrait;function index() {- $cart = ShoppingCart::getInstance($this->global);+ $options = array_intersect_key($this->global['site'],+ array_flip(['weight_type_id', 'length_type_id', 'currency_id', 'country_id']));++ $cart = ShoppingCart::getInstance($options);$payment = Payment::getInstance();$shipping = Shipping::getInstance();- $this->view->payment = $payment->getMethods();- $this->view->shipping = $shipping->getMethods();+ $this->view->payment = $payment->getMethods([]);+ $this->view->shipping = $shipping->getMethods([]);if (isset($this->request->post['product_id']) &&(isset($this->request->get['route']) && $this->request->get['route'] == 'cart/cart/add')) {@@ -47,11 +50,14 @@ class Cart extends Base {$cart = ['products' => $cart->getAll(),'totals' => $cart->getTotals(),- 'total_items' => $cart->getNoProducts(),- 'total_price' => $cart->getNoProducts(),- 'total' => $cart->getGrandTotal(),+ 'total_items' => $cart->getNoProducts(),+ 'total_weight' => $cart->getWeight(),+ 'total_price' => $cart->getNoProducts(),+ 'total' => $cart->getGrandTotal(),'coupons' => $cart->getCoupons(),'total_formatted' => $cart->getGrandTotalFormatted(),+ 'weight_unit' => $this->global['site']['weight_type'],+ 'length_unit' => $this->global['site']['length_type']];$this->view->cart = $cart;diff --git a/app/sql/mysqli/length_type.sql b/app/sql/mysqli/length_type.sqlnew file mode 100644index 0000000..736b16a--- /dev/null+++ b/app/sql/mysqli/length_type.sql@@ -0,0 +1,4 @@+-- Length type++ -- get common code from admin+ import(/admin/length_type.sql);diff --git a/app/sql/mysqli/weight_type.sql b/app/sql/mysqli/weight_type.sqlnew file mode 100644index 0000000..509ae22--- /dev/null+++ b/app/sql/mysqli/weight_type.sql@@ -0,0 +1,4 @@+-- Weight type++ -- get common code from admin+ import(/admin/weight_type.sql);diff --git a/app/sql/pgsql/length_type.sql b/app/sql/pgsql/length_type.sqlnew file mode 100644index 0000000..736b16a--- /dev/null+++ b/app/sql/pgsql/length_type.sql@@ -0,0 +1,4 @@+-- Length type++ -- get common code from admin+ import(/admin/length_type.sql);diff --git a/app/sql/pgsql/weight_type.sql b/app/sql/pgsql/weight_type.sqlnew file mode 100644index 0000000..509ae22--- /dev/null+++ b/app/sql/pgsql/weight_type.sql@@ -0,0 +1,4 @@+-- Weight type++ -- get common code from admin+ import(/admin/weight_type.sql);diff --git a/app/sql/sqlite/length_type.sql b/app/sql/sqlite/length_type.sqlnew file mode 100644index 0000000..736b16a--- /dev/null+++ b/app/sql/sqlite/length_type.sql@@ -0,0 +1,4 @@+-- Length type++ -- get common code from admin+ import(/admin/length_type.sql);diff --git a/app/sql/sqlite/weight_type.sql b/app/sql/sqlite/weight_type.sqlnew file mode 100644index 0000000..509ae22--- /dev/null+++ b/app/sql/sqlite/weight_type.sql@@ -0,0 +1,4 @@+-- Weight type++ -- get common code from admin+ import(/admin/weight_type.sql);diff --git a/system/cart/cart.php b/system/cart/cart.phpindex f45fb0c..4242cf6 100755--- a/system/cart/cart.php+++ b/system/cart/cart.php@@ -38,6 +38,8 @@ class Cart {protected $tax;+ protected $weight;+protected $productModel = 'product';protected $sessionKey = 'cart';@@ -72,8 +74,9 @@ class Cart {$this->session = Session :: getInstance();$this->currency = Currency :: getInstance($options);$this->tax = Tax :: getInstance();+ $this->weight = Weight :: getInstance();- $this->options = $options;+ $this->options = $options;$this->read();if (! isset($this->total_items)) {@@ -177,6 +180,24 @@ class Cart {$value['price_formatted'] = $this->currency->format($value['price']);}+ if ($value['weight']) {+ if ($value['weight_operator'] == '-') {+ $value['weight'] = -$value['weight'];+ }++ $prod['weight'] += $value['weight'];+ $value['weight_formatted'] = $this->currency->format($value['weight']);+ }++ if ($value['points']) {+ if ($value['points_operator'] == '-') {+ $value['points'] = -$value['points'];+ }++ $prod['points'] += $value['points'];+ $value['points_formatted'] = $this->currency->format($value['points']);+ }+$prod['option_value'][$product_option_value_id] = $value;}}@@ -202,8 +223,8 @@ class Cart {//$prod['subscription_plan_id'] = $products[$productId]['subscription_plan_id'];}- $this->total += $prod['total'];- $this->total_tax += $prod['total_tax'];+ $this->total += $prod['total'];+ $this->total_tax += $prod['total_tax'];$this->total_items += $prod['quantity'];$prod = array_merge($prod, $product);@@ -282,7 +303,14 @@ class Cart {}function getNoProducts() {- return count($this->products ?? []);+ //return count($this->products ?? []);+ $total = 0;++ foreach ($this->products as $product) {+ $total += $product['quantity'];+ }++ return $total;}function remove($key) {@@ -307,8 +335,8 @@ class Cart {$weight = 0;foreach ($this->products as $product) {- if ($product['shipping']) {- $weight += $this->weight->convert($product['weight'], $product['weight_type_id'], $this->config->get('config_weight_type_id'));+ if ($product['requires_shipping']) {+ $weight += $this->weight->convert($product['weight'], $product['weight_type_id'], $this->options['weight_type_id']);}}@@ -362,17 +390,6 @@ class Cart {return $this->totals;}- /*- public function getTotal() {- $total = 0;-- foreach ($this->products as $product) {- $total += $this->tax->calculate($product['price'], $product['tax_type_id'], $this->config->get('config_tax')) * $product['quantity'];- }-- return $total;- }- */public function countProducts() {$product_total = 0;diff --git a/system/cart/weight.php b/system/cart/weight.phpnew file mode 100644index 0000000..53f4c6c--- /dev/null+++ b/system/cart/weight.php@@ -0,0 +1,88 @@+<?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;++use Vvveb\Sql\Weight_typeSQL;+use Vvveb\System\Cache;++class Weight {+ private $weightRates = [];++ public static function getInstance($options = []) {+ static $inst = null;++ if ($inst === null) {+ $inst = new Weight($options);+ }++ return $inst;+ }++ public function __construct($options = []) {+ $this->setWeight();+ }++ public function setWeight() {+ $cache = Cache::getInstance();+ $weights = $cache->cache(APP,'weight_type' ,function () {+ $weightType = new Weight_typeSQL();+ $weights = $weightType->getAll(['start' => 0, 'limit' => 100]);++ return $weights['weight_type'] ?? [];+ }, 259200);+ }++ public function clear() {+ $this->weightRates = [];+ }++ public function convert($value, $from, $to) : float {+ if ($from == $to) {+ return $value;+ }++ if (isset($this->weightRates[$from])) {+ $from = $this->weightRates[$from]['value'];+ } else {+ $from = 1;+ }++ if (isset($this->weightRates[$to])) {+ $to = $this->weightRates[$to]['value'];+ } else {+ $to = 1;+ }++ return $value * ($to / $from);+ }++ public function format($value, $weightTypeId, $decimalPoint = '.', $thousandPoint = ',') {+ $unit = $this->weightRates[$weightTypeId]['unit'] ?? '';++ return number_format($value, 2, $decimalPoint, $thousandPoint) . $unit;+ }++ public function getUnit($weightTypeId) {+ $this->weightRates[$weightTypeId]['unit'] ?? '';+ }+}diff --git a/system/functions.php b/system/functions.phpindex 12fbbf3..6ce040c 100755--- a/system/functions.php+++ b/system/functions.php@@ -442,6 +442,9 @@ function cleanUrl($text, $divider = '-') {}function slugify($text, $divider = '-') {+ if (! $text) {+ return $text;+ }// replace non letter or digits by divider$text = preg_replace('/[^\pL\d]+/u', $divider, $text);@@ -1248,6 +1251,7 @@ function siteSettings($site_id = SITE_ID, $language_id = false) {if ($site && isset($site['settings'])) {$settings = json_decode($site['settings'], true);+ $siteData = $siteSql->getSiteData(['site' => $settings]);foreach (['favicon', 'logo', 'logo-sticky', 'logo-dark', 'logo-dark-sticky'] as $img) {if (isset($settings[$img])) {@@ -1256,7 +1260,7 @@ function siteSettings($site_id = SITE_ID, $language_id = false) {}}- return $settings;+ return $settings + $siteData;}return [];