Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Persistent cart, encrypted cart id, product variants support

givanz034942c5db64
diff --git a/system/cart/cart.php b/system/cart/cart.php
index 8c4138a..91a089d 100755
--- a/system/cart/cart.php
+++ b/system/cart/cart.php
@@ -22,9 +22,11 @@
namespace Vvveb\System\Cart;
+use function \Vvveb\getCurrency;
use function \Vvveb\model;
use function \Vvveb\url;
use Vvveb\Sql\Product_Option_ValueSQL;
+use Vvveb\Sql\Product_VariantSQL;
use Vvveb\Sql\ProductSQL;
use Vvveb\Sql\Subscription_PlanSQL;
use Vvveb\System\Images;
@@ -43,8 +45,14 @@ class Cart {
protected $productModel = 'product';
+ protected $cart_id;
+
+ protected $encrypted_cart_id;
+
protected $sessionKey = 'cart';
+ protected $persist = 'session';
+
protected $options;
protected $products = [];
@@ -59,7 +67,9 @@ class Cart {
protected $total_items = 0;
- use TaxTrait, ProductOptionTrait, CouponTrait, TotalTrait;
+ protected $controlCache = true;
+
+ use TaxTrait, ProductOptionTrait, CouponTrait, TotalTrait, DbTrait;
public static function getInstance($options = []) {
static $inst = null;
@@ -77,7 +87,20 @@ class Cart {
$this->tax = Tax :: getInstance();
$this->weight = Weight :: getInstance();
+ $this->cart_id = $options['cart_id'] ?? null;
$this->options = $options;
+
+ if ($this->cart_id && ! is_numeric($this->cart_id)) {
+ $cart_id = urldecode($this->cart_id);
+ $key = \Vvveb\getConfig('app.key');
+ $cart_id = \Vvveb\decrypt($key, $cart_id);
+
+ if ($cart_id) {
+ $this->encrypted_cart_id = $this->cart_id;
+ $this->cart_id = $cart_id;
+ }
+ }
+
$this->read();
if (! isset($this->total_items)) {
@@ -91,17 +114,69 @@ class Cart {
if (! isset($this->products)) {
$this->products = [];
}
+
+ if ($this->total_items) {
+ $this->disableCache();
+ } else {
+ $this->enableCache();
+ }
+ }
+
+ public function create() {
+ //calll only if you need a cart id beforehand, otherwise a cart will be created automatically when adding a product if it does not exist
+ $this->write();
+
+ return $this->cart_id;
+ }
+
+ public function getEncryptedId() {
+ if ($this->encrypted_cart_id) {
+ return $this->encrypted_cart_id;
+ }
+
+ $key = \Vvveb\getConfig('app.key');
+ $this->encrypted_cart_id = \Vvveb\encrypt($key, $this->cart_id);
+
+ return $this->encrypted_cart_id;
+ }
+
+ public function getCartUrl() {
+ $id = urlencode($this->getEncryptedId());
+ $url = url('cart/cart/index', ['cart_id' => $id]);
+ //app does not have url
+ if (! $url) {
+ $url = "/checkout/$id";
+ }
+
+ return $url;
+ }
+
+ public function getCheckoutUrl() {
+ $id = urlencode($this->getEncryptedId());
+ $url = url('checkout/checkout/index', ['cart_id' => $id]);
+ //app does not have url
+ if (! $url) {
+ $url = "/checkout/$id";
+ }
+
+ return $url;
+ }
+
+ public function getId() {
+ return $this->cart_id;
}
public function updateCart() {
$this->total = 0;
$this->total_items = 0;
- $results = ['products' => [], 'count' => 0];
+ $results = ['products' => [], 'count' => 0];
+ $currency = getCurrency();
if (! empty($this->products)) {
$productIds = [];
$productOptions = [];
+ $productVariants = [];
$productSubscriptions = [];
foreach ($this->products as $product) {
@@ -109,7 +184,7 @@ class Cart {
$productIds[$productId] = $productId;
//get all product options to make one query to get all option values
- if (isset($product['option'])) {
+ if (isset($product['option']) && is_array($product['option'])) {
foreach ($product['option'] as $value) {
if (is_numeric($value)) {
$productOptions[$value] = $value;
@@ -121,7 +196,11 @@ class Cart {
}
if (isset($product['subscription_plan_id']) && $product['subscription_plan_id']) {
- $productSubscriptions[$productId] = $product['subscription_plan_id'];
+ $productSubscriptions[$product['subscription_plan_id']] = $product['subscription_plan_id'];
+ }
+
+ if (isset($product['product_variant_id']) && $product['product_variant_id']) {
+ $productVariants[$product['product_variant_id']] = $product['product_variant_id'];
}
}
@@ -149,11 +228,21 @@ class Cart {
$subscriptionResults = [];
if ($productSubscriptions) {
- $subscriptionPlanSql = new Subscription_PlanSQL();
- $subscriptionResults = $subscriptionPlanSql->getAll(
+ $subscriptionPlanSql = new Subscription_PlanSQL();
+ $subscriptionResults = $subscriptionPlanSql->getAll(
['subscription_plan_id' => array_values($productSubscriptions)] + $this->options
)['subscription_plan'] ?? [];
}
+
+ // if products have variants get all variants in one query
+ $variantsResults = [];
+
+ if ($productVariants) {
+ $productVariantSql = new Product_VariantSQL();
+ $variantsResults = $productVariantSql->getAll(
+ ['product_variant_id' => array_values($productVariants)] + $this->options
+ )['product_variant'] ?? [];
+ }
}
$products = $results['product'] ?? [];
@@ -169,10 +258,10 @@ class Cart {
$productId = $prod['product_id'];
$product = $products[$productId];
- $prod['price'] = $product['price'];
+ $prod['price'] = (float)$product['price'];
//add option value data and adjust price if necessary
- if (isset($prod['option'])) {
+ if (isset($prod['option']) && is_array($prod['option'])) {
foreach ($prod['option'] as $option_id => $option) {
if (is_numeric($option)) {
$product_option_value_id = $option;
@@ -184,29 +273,31 @@ class Cart {
if ($value['price']) {
if ($value['price_operator'] == '-') {
- $value['price'] = -$value['price'];
+ $value['price'] -= (float)$value['price'];
}
- $prod['price'] += $value['price'];
- $value['price_formatted'] = $this->currency->format($value['price']);
+ $prod['price'] += (float) $value['price'];
+
+ $value['price_tax'] = $this->tax->addTaxes($value['price'], $product['tax_type_id'] ?? 0);
+ $value['price_formatted'] = $value['price_operator'] . $this->currency->format($value['price_tax']);
}
if ($value['weight']) {
if ($value['weight_operator'] == '-') {
- $value['weight'] = -$value['weight'];
+ $value['weight'] -= $value['weight'];
}
- $prod['weight'] += $value['weight'];
- $value['weight_formatted'] = $this->currency->format($value['weight']);
+ $prod['weight'] = ($prod['weight'] ?? 0) + $value['weight'];
+ $value['weight_formatted'] = $value['weight'];
}
if ($value['points']) {
if ($value['points_operator'] == '-') {
- $value['points'] = -$value['points'];
+ $value['points'] -= $value['points'];
}
$prod['points'] += $value['points'];
- $value['points_formatted'] = $this->currency->format($value['points']);
+ $value['points_formatted'] = $value['points'];
}
$prod['option_value'][$product_option_value_id] = $value;
@@ -216,20 +307,33 @@ class Cart {
//add subscription data
if (isset($prod['subscription_plan_id']) && $prod['subscription_plan_id']) {
$prod['subscription'] = $subscriptionResults[$prod['subscription_plan_id']] ?? [];
- $prod['subscription_name'] = $prod['subscription']['name'];
+ $prod['subscription_name'] = $prod['subscription']['name'] ?? '';
+ }
+
+ //if product variant override price and stock
+ if (isset($prod['product_variant_id']) && $prod['product_variant_id']) {
+ $variant = $variantsResults[$prod['options']] ?? [];
+
+ if ($variant && $variant['product_variant_id'] == $prod['product_variant_id']) {
+ $prod['variant'] = $variant;
+ $prod['price'] = $variant['price'];
+ $prod['stock_quantity'] = $variant['stock_quantity'];
+ }
}
$url = ['slug' => $product['slug'], 'product_id' => $product['product_id']];
- $prod['url'] = htmlentities(url('product/product/index', $url));
- $prod['remove-url'] = htmlentities(url('cart/cart/remove', $url));
+ $prod['key'] = $key;
+ $prod['url'] = htmlspecialchars(url('product/product/index', $url));
+ $prod['remove-url'] = htmlspecialchars(url('cart/cart/remove', $url + ['key' => $key]));
- $prod['total'] = (int)$prod['price'] * $prod['quantity'];
+ $prod['total'] = $prod['price'] * $prod['quantity'];
$prod['total_formatted'] = $this->currency->format($prod['total']);
- $taxValue = $this->tax->addTaxes($prod['price'], $product['tax_type_id'], true);
- $prod['price_tax'] = ($taxValue + $prod['price']);
- $prod['tax'] = $prod['price_tax'] * $prod['quantity'];
- $prod['total_tax'] = $prod['tax'];
+ $taxValue = $this->tax->addTaxes($prod['price'], $product['tax_type_id'], true);
+ $prod['price_tax'] = ($taxValue + $prod['price']);
+ $prod['tax'] = $prod['price_tax'] * $prod['quantity'];
+ $prod['total_tax'] = $prod['tax'];
+ $prod['price_currency'] = $currency;
$prod['price_tax_formatted'] = $this->currency->format($prod['price_tax']);
$prod['price_formatted'] = $this->currency->format($product['price']);
@@ -244,12 +348,14 @@ class Cart {
$this->total_tax += $prod['total_tax'];
$this->total_items += $prod['quantity'];
- $prod = array_merge($prod, $product);
+ $prod = array_replace($product, $prod);
if (isset($product['image'])) {
$prod['image'] = Images::image($product['image'], 'product', 'thumb');
}
+ unset($prod['content']);
+
// options add to price
}
}
@@ -258,9 +364,9 @@ class Cart {
//set cart cookie to disable cache if products in cart
if ($this->total_items) {
- setcookie('cart', '1', 0, '/');
+ $this->disableCache();
} else {
- setcookie('cart', '', time() - 3600, '/');
+ $this->enableCache();
}
//write is done by addTotal
//$this->write();
@@ -268,15 +374,20 @@ class Cart {
return $results;
}
- function add($productId, $quantity = 1, $option = [], $subscriptionPlanId = false) {
+ function add($productId, $quantity = 1, $option = [], $productVariantId = false, $subscriptionPlanId = false) {
if (! $productId) {
return false;
}
- $key = $productId;
+ $key = $productId;
+ $options = '';
- if ($option) {
- $key .= '_' . json_encode($option);
+ if ($option && is_array($option)) {
+ ksort($option);
+ $options = trim(str_replace('"', '', json_encode($option)), '{}');
+ $key .= '_' . $options;
+ } else {
+ $option = [];
}
if ($subscriptionPlanId) {
@@ -291,15 +402,19 @@ class Cart {
$this->products[$key] = [
'product_id' => (int) $productId,
'quantity' => (int)$quantity,
- 'option' => $option,
+ 'option' => $option ?? [],
+ 'options' => $options,
'subscription_plan_id' => $subscriptionPlanId,
+ 'product_variant_id' => $productVariantId,
];
}
- return $this->updateCart();
+ $this->updateCart();
+
+ return $key;
}
- function update($key, $quantity = 1, $option = [], $subscriptionPlanId = false) {
+ function update($key, $quantity = 1, $option = [], $productVariantId = false, $subscriptionPlanId = false) {
if (isset($this->products[$key])) {
$this->products[$key]['quantity'] = max(1, $quantity);
@@ -311,8 +426,16 @@ class Cart {
$this->products[$key]['subscription_plan_id'] = $subscriptionPlanId;
}
- return $this->updateCart();
+ if ($productVariantId) {
+ $this->products[$key]['product_variant_id'] = $productVariantId;
+ }
+
+ $this->updateCart();
+
+ return $key;
}
+
+ return false;
}
function getAll() {
@@ -331,9 +454,25 @@ class Cart {
}
function remove($key) {
- unset($this->products[$key]);
+ if ($key) {
+ if (is_array($key)) {
+ foreach ($key as $value) {
+ if (isset($this->products[$value])) {
+ unset($this->products[$value]);
+ }
+ }
+ } else {
+ if (isset($this->products[$key])) {
+ unset($this->products[$key]);
+ }
+ }
+
+ $this->updateCart();
+
+ return $key;
+ }
- return $this->updateCart();
+ return false;
}
public function getSubscription() {
@@ -410,28 +549,42 @@ class Cart {
return false;
}
- protected function read() {
- $data = $this->session->get($this->sessionKey);
+ /*
+ protected function read() {
+ $data = $this->session->get($this->sessionKey);
- if (is_array($data)) {
- foreach ($data as $property => $value) {
- $this->$property = $value;
+ if (is_array($data)) {
+ foreach ($data as $property => $value) {
+ $this->$property = $value;
+ }
}
}
- }
- protected function write() {
- //$data = get_object_vars($this);
- foreach (['products', 'taxes', 'totals', 'total_items', 'coupons', 'product_options'] as $property) {
- $data[$property] = $this->$property;
+ protected function write() {
+ //$data = get_object_vars($this);
+ foreach (['products', 'taxes', 'totals', 'total_items', 'coupons', 'product_options'] as $property) {
+ $data[$property] = $this->$property;
+ }
+
+ $this->session->set($this->sessionKey, $data);
}
- $this->session->set($this->sessionKey, $data);
+ public function empty() {
+ $this->session->set($this->sessionKey, []);
+ $this->enableCache();
+ }
+ */
+ public function enableCache() {
+ //enable cache back by clearing the cart cookie
+ if ($this->controlCache && isset($_COOKIE['cart'])) {
+ setcookie('cart', '', time() - 3600, '/');
+ }
}
- public function empty() {
- $this->session->set($this->sessionKey, []);
- //enable cache back by clearing the cart cookie
- setcookie('cart', '', time() - 3600, '/');
+ public function disableCache() {
+ //disable cache back setting the cart cookie
+ if ($this->controlCache && ! isset($_COOKIE['cart'])) {
+ setcookie('cart', '1', 0, '/');
+ }
}
}
diff --git a/system/cart/currency.php b/system/cart/currency.php
index 505bda6..6ef47dc 100755
--- a/system/cart/currency.php
+++ b/system/cart/currency.php
@@ -26,8 +26,6 @@ use function Vvveb\availableCurrencies;
use function Vvveb\session as sess;
class Currency {
- private $driver;
-
private $currencies;
private $options;
diff --git a/system/cart/db-trait.php b/system/cart/db-trait.php
new file mode 100644
index 0000000..79e246c
--- /dev/null
+++ b/system/cart/db-trait.php
@@ -0,0 +1,129 @@
+<?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\CartSQL;
+
+trait DbTrait {
+ private $cartTable;
+
+ protected function initDb() {
+ $this->cartTable = new CartSQL();
+ }
+
+ protected function getDbCart() {
+ $data = [];
+
+ if (! $this->cartTable) {
+ $this->initDb();
+ }
+
+ if ($cart = $this->cartTable->get(['cart_id' => $this->cart_id/*, 'user_id' => $this->userId*/])) {
+ if (isset($cart['data']) && $cart['data']) {
+ $data = json_decode($cart['data'], true);
+ }
+ }
+
+ return $data;
+ }
+
+ function loadCart($cart_id) {
+ $data = [];
+
+ if ($cart_id) {
+ $this->cart_id = $cart_id;
+ $data = $this->getDbCart();
+ }
+
+ if (is_array($data) && $data) {
+ foreach ($data as $property => $value) {
+ $this->$property = $value;
+ }
+
+ $this->updateCart();
+ }
+
+ return $data;
+ }
+
+ protected function read() {
+ $data = $this->session->get($this->sessionKey) ?? [];
+ $this->cart_id = $this->cart_id ?? $data['cart_id'] ?? null;
+
+ if (! $data && $this->cart_id) {
+ $data = $this->getDbCart();
+ }
+
+ if (is_array($data) && $data) {
+ foreach ($data as $property => $value) {
+ $this->$property = $value;
+ }
+ }
+
+ return $data;
+ }
+
+ protected function write() {
+ $data = [];
+
+ foreach (['products', 'taxes', 'totals', 'total_items', 'coupons', 'product_options'] as $property) {
+ $data[$property] = $this->$property;
+ }
+
+ $json = json_encode($data);
+
+ $cart = [];
+ $cart['data'] = $json;
+
+ if (! $this->cartTable) {
+ $this->initDb();
+ }
+
+ if ($this->cart_id) {
+ if ($return = $this->cartTable->edit(['cart_id' => $this->cart_id, 'cart' => $cart])) {
+ }
+ } else {
+ if ($return = $this->cartTable->add(['cart' => $cart])) {
+ $this->cart_id = $return['cart'];
+ }
+ }
+
+ $data['cart_id'] = $this->cart_id;
+
+ $this->session->set($this->sessionKey, $data);
+ }
+
+ public function empty() {
+ if (! $this->cartTable) {
+ $this->initDb();
+ }
+
+ $this->session->set($this->sessionKey, []);
+
+ if ($this->cart_id && $this->cartTable->delete(['cart_id' => [$this->cart_id]])) {
+ $this->cart_id = null;
+
+ return true;
+ }
+ }
+}
diff --git a/system/cart/order-cart.php b/system/cart/order-cart.php
index bcd9958..df8ad89 100644
--- a/system/cart/order-cart.php
+++ b/system/cart/order-cart.php
@@ -22,54 +22,37 @@
namespace Vvveb\System\Cart;
-use function \Vvveb\model;
-use function \Vvveb\url;
use Vvveb\Sql\OrderSQL;
-use Vvveb\Sql\Product_option_valueSQL;
-use Vvveb\Sql\ProductSQL;
-use Vvveb\System\Images;
use Vvveb\System\Session;
class OrderCart extends Cart {
- protected $cart = [];
-
- protected $session;
-
- protected $currency;
-
- protected $tax;
-
protected $order_id;
- protected $productModel = 'product';
-
- protected $options;
-
- protected $order = [];
-
- protected $products = [];
-
- protected $taxes = [];
-
- protected $totals = [];
-
- protected $total = 0;
-
- protected $total_tax = 0;
-
- protected $total_items = 0;
-
- use TaxTrait, ProductOptionTrait, CouponTrait;
+ protected $controlCache = false;
function __construct($order_id, $options = []) {
$this->session = Session :: getInstance();
$this->currency = Currency :: getInstance($options);
$this->tax = Tax :: getInstance();
+ $this->weight = Weight :: getInstance();
$this->tax->setRegionRules($options['country_id'], $options['region_id']);
$this->order_id = $order_id;
+ $this->cart_id = $options['cart_id'] ?? null;
$this->options = $options;
+
+ if ($this->cart_id && ! is_numeric($this->cart_id)) {
+ $cart_id = urldecode($this->cart_id);
+ $key = \Vvveb\getConfig('app.key');
+ $cart_id = \Vvveb\decrypt($key, $cart_id);
+
+ if ($cart_id) {
+ $this->encrypted_cart_id = $this->cart_id;
+ $this->cart_id = $cart_id;
+ }
+ }
+
$this->read();
if (! isset($this->total_items)) {
@@ -85,139 +68,7 @@ class OrderCart extends Cart {
}
}
- public function updateCart() {
- $this->total = 0;
- $this->total_items = 0;
-
- $results = ['products' => [], 'count' => 0];
-
- if (! empty($this->products)) {
- $productIds = [];
- $productOptions = [];
- $productSubscriptions = [];
-
- foreach ($this->products as $product) {
- $productId = $product['product_id'];
- $productIds[$productId] = $productId;
-
- //get all product options to make one query to get all option values
- if (isset($product['option'])) {
- foreach ($product['option'] as $value) {
- if (is_numeric($value)) {
- $productOptions[$value] = $value;
- } else {
- $product_option_value_id = $value['product_option_value_id'];
- $productOptions[$product_option_value_id] = $product_option_value_id;
- }
- }
- }
-
- if (isset($product['subscription_plan_id'])) {
- $productSubscriptions[$productId] = $product['subscription_plan_id'];
- }
- }
-
- //get product data from db for products in cart
- $options = [
- 'product_id' => $productIds,
- ] + $this->options;
-
- $productSql = model($this->productModel); //new ProductSQL();
- $results = $productSql->getAll(
- $options
- );
-
- // if products have options get all product options in one query
- $optionResults = [];
-
- if ($productOptions) {
- $productOptionValueSql = new Product_option_valueSQL();
- $optionResults = $productOptionValueSql->getAll(
- ['product_option_value_id' => array_values($productOptions)] + $this->options
- )['product_option_value'] ?? [];
- }
- }
-
- $products = $results['product'] ?? [];
-
- if ($products) {
- foreach ($this->products as $key => &$prod) {
- if (! isset($products[$product['product_id']])) {
- unset($this->products[$key]);
-
- continue;
- }
-
- $productId = $prod['product_id'];
- $product = $products[$productId];
-
- $prod['price'] = $product['price'];
-
- //add option value data and adjust price if necessary
- if (isset($prod['option'])) {
- foreach ($prod['option'] as $option_id => $option) {
- if (is_numeric($option)) {
- $product_option_value_id = $option;
- } else {
- $product_option_value_id = $option['product_option_value_id'];
- }
-
- $value = $optionResults[$product_option_value_id];
-
- if ($value['price']) {
- if ($value['price_operator'] == '-') {
- $value['price'] = -$value['price'];
- }
-
- $prod['price'] += $value['price'];
- $value['price_formatted'] = $this->currency->format($value['price']);
- }
-
- $prod['option_value'][$product_option_value_id] = $value;
- }
- }
-
- $prod['url'] = htmlentities(url('product/product/index', $product));
- $prod['remove-url'] = htmlentities(url('cart/cart/remove', $product));
-
- $prod['total'] = (int)$prod['price'] * $prod['quantity'];
- $prod['total_formatted'] = $this->currency->format($prod['total']);
-
- $taxValue = $this->tax->addTaxes($prod['price'], $product['tax_type_id'], true);
- $prod['price_tax'] = ($taxValue + $prod['price']);
- $prod['tax'] = $prod['price_tax'] * $prod['quantity'];
- $prod['total_tax'] = $prod['tax'];
-
- $prod['price_tax_formatted'] = $this->currency->format($prod['price_tax']);
- $prod['price_formatted'] = $this->currency->format($product['price']);
- $prod['total_formatted'] = $this->currency->format($prod['total']);
- $prod['total_tax_formatted'] = $this->currency->format($prod['total_tax']);
-
- if (isset($products[$productId]['subscription_plan_id'])) {
- //$prod['subscription_plan_id'] = $products[$productId]['subscription_plan_id'];
- }
-
- $this->total += $prod['total'];
- $this->total_tax += $prod['total_tax'];
- $this->total_items += $prod['quantity'];
-
- $prod = array_merge($prod, $product);
-
- if (isset($product['image'])) {
- $prod['image'] = Images::image($product['image'], 'product', 'thumb');
- }
-
- // options add to price
- }
- }
-
- $this->addTotal('sub_total', 'Sub-total', $this->total);
- //write is done by addTotal
- //$this->write();
- return $results;
- }
-
- function add($productId, $orderProductId = false, $quantity = 1, $option = [], $subscriptionPlanId = false) {
+ function addOrderProduct($productId, $orderProductId = false, $quantity = 1, $option = [], $subscriptionPlanId = false) {
if (! $productId) {
return false;
}
@@ -275,19 +126,18 @@ class OrderCart extends Cart {
//$this->totals = $results['total'];
if (isset($results['total']) && $results['total']) {
foreach ($results['total'] as $total) {
- $this->addTotal($total['key'], $total['title'], $total['value'], '', $total['order_total_id']);
+ $this->addTotal($total['key'], $total['title'], $total['value'], $total['title'], $total['order_total_id']);
}
}
//$this->products = $results['product'];
- if (isset($results['product']) && $results['product']) {
- foreach ($results['product'] as $product) {
+ if (isset($results['products']) && $results['products']) {
+ foreach ($results['products'] as $product) {
$option_value = $product['option_value'] ? json_decode($product['option_value'], true) : [];
- $this->add($product['product_id'], $product['order_product_id'], $product['quantity'], $option_value);
+ $this->addOrderProduct($product['product_id'], $product['order_product_id'], $product['quantity'], $option_value);
}
}
$this->updateCart();
-
/*
if (is_array($data)) {
@@ -299,7 +149,6 @@ class OrderCart extends Cart {
public function write() {
$this->addTaxTotal();
-
$order_id = $this->order_id;
$orders = new OrderSQL();
diff --git a/system/cart/tax-trait.php b/system/cart/tax-trait.php
index 3ad4a53..841a159 100644
--- a/system/cart/tax-trait.php
+++ b/system/cart/tax-trait.php
@@ -34,20 +34,24 @@ trait TaxTrait {
}
public function getTaxes() {
- $taxes = [];
- $products = $this->products + $this->taxes;
+ $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) {
- if (! isset($taxes[$tax_rate['tax_rate_id']])) {
- $taxes[$tax_rate['tax_rate_id']] = $tax_rate;
- $taxes[$tax_rate['tax_rate_id']]['value'] = 0;
+ $rateId = $tax_rate['tax_rate_id'] ?? false;
+
+ if (! isset($taxes[$rateId])) {
+ $taxes[$rateId] = $tax_rate;
+ $taxes[$rateId]['value'] = 0;
}
- $taxes[$tax_rate['tax_rate_id']]['value'] += ($tax_rate['amount'] * ($product['quantity'] ?? 1));
+ $taxes[$rateId]['value'] += ($tax_rate['amount'] * ($product['quantity'] ?? 1));
+ $this->total_tax = $taxes[$rateId]['value'];
}
}
}
@@ -55,6 +59,10 @@ trait TaxTrait {
return $taxes;
}
+ public function getTaxTotal() {
+ return $this->total_tax;
+ }
+
function addTaxTotal() {
$taxes = $this->getTaxes();
diff --git a/system/cart/tax.php b/system/cart/tax.php
index c069039..e0b386f 100755
--- a/system/cart/tax.php
+++ b/system/cart/tax.php
@@ -26,8 +26,6 @@ use Vvveb\Sql\Tax_typeSQL;
use Vvveb\System\Cache;
class Tax {
- private $driver;
-
private $taxRates = [];
public static function getInstance($options = []) {
@@ -69,10 +67,12 @@ class Tax {
if (isset($this->taxRates[$taxTypeId])) {
foreach ($this->taxRates[$taxTypeId] as $taxRate) {
+ $rate = (float)$taxRate['rate'];
+
if ($taxRate['type'] == 'f') {//fixed
- $amount += $taxRate['rate'];
+ $amount += $rate;
} elseif ($taxRate['type'] == 'p') {//percent
- $amount += ($value / 100 * $taxRate['rate']);
+ $amount += (($value / 100) * $rate);
}
}
}
@@ -94,11 +94,13 @@ class Tax {
} else {
$amount = 0;
}
+ $rate = (float)$taxRate['rate'];
+ $value = (float)$value;
if ($taxRate['type'] == 'f') {
- $amount += $taxRate['rate'];
+ $amount += $rate;
} elseif ($taxRate['type'] == 'p') {
- $amount += ($value / 100 * $taxRate['rate']);
+ $amount += (($value / 100) * $rate);
}
$taxRate_data[$taxRate['tax_rate_id']] = [
diff --git a/system/cart/total-trait.php b/system/cart/total-trait.php
index faa5ea4..99dd352 100644
--- a/system/cart/total-trait.php
+++ b/system/cart/total-trait.php
@@ -59,7 +59,7 @@ trait TotalTrait {
if ($this->totals) {
foreach ($this->totals as $total) {
- $sum += $total['value'] ?? 0;
+ $sum += (float)($total['value'] ?? 0);
}
}