Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Added product variant support

givanz705209d566bd
diff --git a/app/controller/cart/cart.php b/app/controller/cart/cart.php
index 7e9784a..fc4279e 100755
--- a/app/controller/cart/cart.php
+++ b/app/controller/cart/cart.php
@@ -28,8 +28,10 @@ use Vvveb\System\Core\View;
use Vvveb\System\Event;
use Vvveb\System\Payment;
use Vvveb\System\Shipping;
+//use Vvveb\Trait\Cart as CartTrait;
class Cart extends Base {
+// use CartTrait;
use CouponTrait;
private $cart;
@@ -40,7 +42,17 @@ class Cart extends Base {
$options = array_intersect_key($this->global['site'],
array_flip(['weight_type_id', 'length_type_id', 'currency_id', 'country_id']));
+ $cart_id = false;
+
+ if (isset($this->request->get['cart_id'])) {
+ $cart_id = $options['cart_id'] = $this->request->get['cart_id'];
+ }
+
$this->cart = ShoppingCart::getInstance($this->global + $options);
+
+ if ($cart_id) {
+ $this->cart->loadCart($cart_id);
+ }
}
function index() {
@@ -50,42 +62,46 @@ class Cart extends Base {
$this->view->payment = $payment->getMethods([]);
$this->view->shipping = $shipping->getMethods([]);
- if (isset($this->request->post['product_id']) &&
- (isset($this->request->get['module']) && $this->request->get['module'] == 'cart/cart/add')) {
- $this->cart->add($this->request->post['product_id']);
+ $product_id = $this->request->post['product_id'] ?? $this->request->get['product_id'] ?? false;
+ $module = $this->request->get['module'] ?? $this->request->post['module'] ?? '';
+
+ if ($product_id && ($module == 'cart/cart/add' || $module == 'cart/cart')) {
+ $this->action('add', $product_id);
}
$cart = [
- 'products' => $this->cart->getAll(),
- 'totals' => $this->cart->getTotals(),
- 'total_items' => $this->cart->getNoProducts(),
- 'total_weight' => $this->cart->getWeight(),
- 'total_price' => $this->cart->getNoProducts(),
- 'total' => $this->cart->getGrandTotal(),
- 'coupons' => $this->cart->getCoupons(),
- 'total_formatted' => $this->cart->getGrandTotalFormatted(),
- 'weight_unit' => $this->global['site']['weight_type'],
- 'length_unit' => $this->global['site']['length_type'],
+ 'products' => $this->cart->getAll(),
+ 'totals' => $this->cart->getTotals(),
+ 'total_items' => $this->cart->getNoProducts(),
+ 'total_weight' => $this->cart->getWeight(),
+ 'total_price' => $this->cart->getNoProducts(),
+ 'total' => $this->cart->getGrandTotal(),
+ 'coupons' => $this->cart->getCoupons(),
+ 'total_formatted' => $this->cart->getGrandTotalFormatted(),
+ 'cart_id' => ($cardId = $this->cart->getId()),
+ 'encrypted_cart_id' => $cardId ? $this->cart->getId() : '',
+ 'weight_unit' => $this->global['site']['weight_type'],
+ 'length_unit' => $this->global['site']['length_type'],
];
$this->view->cart = $cart;
}
private function action($action, $productId = null, $quantity = 1) {
- $productId = $this->request->request['product_id'] ?? false;
+ $productId = $this->request->request['product_id'] ?? $productId ?? false;
$key = $this->request->request['key'] ?? false;
$quantity = $this->request->request['quantity'] ?? $quantity;
$option = $this->request->request['option'] ?? [];
$subscriptionPlanId = $this->request->request['subscription_plan_id'] ?? false;
+ $productVariantId = $this->request->request['product_variant_id'] ?? false;
list($action, $productId, $key, $quantity, $option, $subscriptionPlanId) =
Event :: trigger(__CLASS__,__FUNCTION__, $action, $productId, $key, $quantity, $option, $subscriptionPlanId);
if ($key || $productId) {
- //$this->view->success = false;
switch ($action) {
case 'add':
- $this->cart->add($productId, $quantity, $option, $subscriptionPlanId);
+ $this->cart->add($productId, $quantity, $option, $productVariantId, $subscriptionPlanId);
break;
@@ -103,19 +119,21 @@ class Cart extends Base {
}
$this->view->noJson = true;
-
- return $this->index();
}
function remove() {
- return $this->action('remove');
+ $this->action('remove');
+
+ return $this->index();
}
function update() {
- return $this->action('update');
+ $this->action('update');
+
+ return $this->index();
}
function add() {
- return $this->action('add');
+ return $this->index();
}
}
diff --git a/app/controller/checkout/checkout.php b/app/controller/checkout/checkout.php
index f310239..87829ae 100755
--- a/app/controller/checkout/checkout.php
+++ b/app/controller/checkout/checkout.php
@@ -46,6 +46,8 @@ use function Vvveb\url;
class Checkout extends Base {
use LoginTrait, CouponTrait;
+ private $cart;
+
/*
use LoginTrait {
login as protected loginBase;
@@ -59,6 +61,25 @@ class Checkout extends Base {
}
*/
+ function init() {
+ parent::init();
+
+ $options = array_intersect_key($this->global['site'],
+ array_flip(['weight_type_id', 'length_type_id', 'currency_id', 'country_id']));
+
+ $cart_id = false;
+
+ if (isset($this->request->get['cart_id'])) {
+ $cart_id = $options['cart_id'] = $this->request->get['cart_id'];
+ }
+
+ $this->cart = Cart::getInstance($this->global + $options);
+
+ if ($cart_id) {
+ $this->cart->loadCart($cart_id);
+ }
+ }
+
function regions() {
$country_id = $this->request->get['country_id'] ?? false;
$regions = [];
@@ -101,20 +122,17 @@ class Checkout extends Base {
}
function index() {
- $options = array_intersect_key($this->global['site'],
- array_flip(['weight_type_id', 'length_type_id', 'currency_id', 'country_id']));
- $cart = Cart :: getInstance($this->global + $options);
-
//buy now product
if (isset($this->request->get['product_id'])) {
$productId = $this->request->get['product_id'];
$quantity = $this->request->post['quantity'] ?? 1;
$option = $this->request->post['option'] ?? [];
$subscriptionPlanId = $this->request->post['subscription_plan_id'] ?? false;
- $cart->add($productId, $quantity, $option, $subscriptionPlanId);
+ $productVariantId = $this->request->post['product_variant_id'] ?? false;
+ $this->cart->add($productId, $quantity, $option, $productVariantId, $subscriptionPlanId);
}
- if (! $cart->hasProducts()) {
+ if (! $this->cart->hasProducts()) {
return $this->redirect('cart/cart/index');
}
@@ -125,8 +143,8 @@ class Checkout extends Base {
$order = Order::getInstance();
$checkoutInfo = $this->session->get('checkout') ?? [];
- $grandTotal = $cart->getGrandTotal();
- $hasShipping = $cart->hasShipping();
+ $grandTotal = $this->cart->getGrandTotal();
+ $hasShipping = $this->cart->hasShipping();
$hasPayment = ($grandTotal > 0);
$this->view->hasShipping = $hasShipping;
$this->view->hasPayment = $hasPayment;
@@ -249,9 +267,9 @@ class Checkout extends Base {
$this->session->set('checkout', $checkoutInfo);
if (($errors = $validator->validate($this->request->post)) === true) {
- $checkoutInfo['products'] = $cart->getAll();
- $checkoutInfo['product_options'] = $cart->getProductOptions();
- $checkoutInfo['totals'] = $cart->getTotals();
+ $checkoutInfo['products'] = $this->cart->getAll();
+ $checkoutInfo['product_options'] = $this->cart->getProductOptions();
+ $checkoutInfo['totals'] = $this->cart->getTotals();
$checkoutInfo['total'] = $grandTotal;
$checkoutInfo += prefixArrayKeys('shipping_', $checkoutInfo['shipping_address']);
$checkoutInfo += prefixArrayKeys('billing_', $checkoutInfo['billing_address']);
@@ -264,8 +282,9 @@ class Checkout extends Base {
foreach (['first_name', 'last_name', 'email', 'phone_number', 'password'] as $field) {
$userInfo[$field] = $checkoutInfo[$field] ?? NULL;
}
+
$userInfo['display_name'] = $userInfo['first_name'] . ' ' . $userInfo['last_name'];
- $userInfo['username'] = str_replace(' ', '', $userInfo['first_name'] . $userInfo['last_name']);
+ $userInfo['username'] = strtolower(str_replace(' ', '', $userInfo['first_name'] . $userInfo['last_name'])) . '-' . rand();
$result = User::add($userInfo);
@@ -346,7 +365,6 @@ class Checkout extends Base {
$this->view->messages[] = __('Order placed!');
$this->session->set('order', $checkoutInfo);
- $cart->empty();
$site = siteSettings();
$shippingData = $checkoutInfo['shipping_data'];
@@ -372,8 +390,9 @@ class Checkout extends Base {
try {
$error = __('Error sending order confirmation mail!');
+ $title = sprintf(__('Order confirmation #%s'), $customer_order_id);
- if (! email([$checkoutInfo['email'], $site['admin-email']], sprintf(__('Order confirmation #%s'), $customer_order_id), 'order/new', $checkoutInfo)) {
+ if (! email([$checkoutInfo['email'], $site['admin-email']], $title, 'order/new', $checkoutInfo + ['title' => $title])) {
$this->session->set('errors', $error);
$this->view->errors[] = $error;
}
@@ -385,6 +404,8 @@ class Checkout extends Base {
$this->view->errors[] = $error;
}
+ $this->cart->empty();
+
// clear notifications cache
CacheManager :: clearObjectCache('component', 'notifications');
diff --git a/app/template/components/product.tpl b/app/template/components/product.tpl
index a2e6ede..384e1c2 100755
--- a/app/template/components/product.tpl
+++ b/app/template/components/product.tpl
@@ -33,6 +33,7 @@ $_pagination_limit = isset($product['limit']) ? $product['limit'] : 5;
@product button[data-v-product-*]|formaction = $product['@@__data-v-product-(*)__@@']
@product a[data-v-product-*]|href = $product['@@__data-v-product-(*)__@@']
+@product [name="product_variant_id"] = $product['product_variant_id']
@product img[data-v-product-main-image]|src = $product['image']
@@ -47,7 +48,7 @@ $_default = (isset($vvveb_is_page_edit) && $vvveb_is_page_edit ) ? [0 => ['produ
$_images = empty($_images) ? $_default : $_images;
if($_images) {
- $i = 1;
+ $i = 0;
foreach ($_images as $index => $_image) { ?>
@images [data-bs-slide-to]|data-bs-slide-to = <?php echo $i ?? 1;?>
@@ -68,4 +69,4 @@ $component = $previous_component;
?>
-
\ No newline at end of file
+