Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Moved code to product trait

givanzcbfa622819b1
diff --git a/app/component/products.php b/app/component/products.php
index bbe34c3..5b31cc8 100755
--- a/app/component/products.php
+++ b/app/component/products.php
@@ -22,15 +22,14 @@
namespace Vvveb\Component;
-use function Vvveb\getCurrency;
-use Vvveb\System\Cart\Currency;
-use Vvveb\System\Cart\Tax;
use Vvveb\System\Component\ComponentBase;
use Vvveb\System\Event;
-use Vvveb\System\Images;
+use Vvveb\System\Traits\Product;
use function Vvveb\url;
class Products extends ComponentBase {
+ use Product;
+
public static $defaultOptions = [
'start' => 0,
'page' => 1,
@@ -51,20 +50,34 @@ class Products extends ComponentBase {
'like' => null,
'slug' => null,
'related' => null,
- 'variant' => null,
+ 'variant' => null, //[true, false] include variants
+ 'promotion' => null, //[true, false] include promotional price
+ 'points' => null, //[true, false] include points
+ 'stock_status' => null, //[true, false] include stock status info
+ 'weight_type' => null, //[true, false] include weight type info
+ 'length_type' => null, //[true, false] include length type info
+ 'rating' => null, //[true, false] include rating average
+ 'reviews' => null, //[true, false] include reviews count
'image_size' => 'medium',
- 'filter' => null,
+ 'filter' => null, //[true, false] include variants
'order_by' => NULL,
- 'direction' => ['url', 'asc', 'desc'],
+ 'variant_price' => NULL,
+ 'direction' => ['desc', 'asc'], //'url','asc', 'desc'
];
+ private $tax;
+
+ private $currency;
+
+ private $currentCurrency;
+
public $options = [];
function results() {
$products = new \Vvveb\Sql\ProductSQL();
- if ($page = $this->options['page']) {
- $this->options['start'] = ($page - 1) * $this->options['limit'];
+ if (($page = $this->options['page']) && is_numeric($page)) {
+ $this->options['start'] = ($page - 1) * ((int) ($this->options['limit'] ?? 4));
}
if ($this->options['filter']) {
@@ -110,55 +123,21 @@ class Products extends ComponentBase {
$this->options['vendor_id'] = [$this->options['vendor_id']];
}
+ //if only one taxonomy_item_id is provided then add it to array
+ if (isset($this->options['taxonomy_item_id']) && ! is_array($this->options['taxonomy_item_id'])) {
+ $this->options['taxonomy_item_id'] = [$this->options['taxonomy_item_id']];
+ }
+
if ($this->options['search'] && $this->options['search_boolean']) {
$this->options['search'] .= '*';
}
- $results = $products->getAll($this->options) + $this->options;
- $currentCurrency = getCurrency();
+ $results = $products->getAll($this->options) + $this->options;
if ($results && isset($results['product'])) {
- $tax = Tax::getInstance($this->options);
- $currency = Currency::getInstance($this->options);
-
- foreach ($results['product'] as $id => &$product) {
- $language = [];
-
- if ($product['language_id'] != $this->options['default_language_id']) {
- $language = ['language' => $this->options['language']];
- }
-
- if (isset($product['images'])) {
- $product['images'] = json_decode($product['images'], true);
-
- foreach ($product['images'] as &$image) {
- $image['image'] = Images::image($image['image'], 'product', $this->options['image_size']);
- }
- }
-
- if (isset($product['image']) && $product['image']) {
- $product['image'] = Images::image($product['image'], 'product', $this->options['image_size']);
- //$product['images'][] = ['image' => Images::image($product['image'], 'product')];
- }
-
- //rfc
- $product['pubDate'] = date('r', strtotime($product['created_at']));
- $product['modDate'] = date('r', strtotime($product['updated_at']));
- $product['lastMod'] = date('Y-m-d\TH:i:sP', strtotime($product['updated_at']));
-
- $url = ['slug' => $product['slug'], 'product_id' => $product['product_id']] + $language;
- $product['url'] = url('product/product/index', $url);
- $product['add_cart_url'] = url('cart/cart/add', ['product_id' => $product['product_id']]);
- $product['buy_url'] = url('checkout/checkout/index', ['product_id' => $product['product_id']]);
- $product['add_wishlist_url'] = url('user/wishlist/add', ['product_id' => $product['product_id']]);
- $product['add_compare_url'] = url('cart/compare/add', ['product_id' => $product['product_id']]);
- $product['full-url'] = url('product/product/index', $url + ['host' => SITE_URL, 'scheme' => $_SERVER['REQUEST_SCHEME'] ?? 'http']);
-
- $product['price_tax'] = $tax->addTaxes($product['price'], $product['tax_type_id']);
- $product['price_tax_formatted'] = $currency->format($product['price_tax']);
- $product['price_formatted'] = $currency->format($product['price']);
- $product['price_currency'] = $currentCurrency;
- }
+ $this->products($results['product'], $this->options);
+ } else {
+ $results['product'] = [];
}
$results['limit'] = $this->options['limit'];
diff --git a/system/traits/product.php b/system/traits/product.php
new file mode 100644
index 0000000..74bbbac
--- /dev/null
+++ b/system/traits/product.php
@@ -0,0 +1,115 @@
+<?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\Traits;
+
+use function Vvveb\__;
+use function Vvveb\getCurrency;
+use Vvveb\System\Cart\Currency;
+use Vvveb\System\Cart\Tax;
+use Vvveb\System\Images;
+use function Vvveb\url;
+
+trait Product {
+ function products(&$products, $options = []) {
+ if (! $products) {
+ return $products;
+ }
+
+ if (! isset($this->currentCurrency)) {
+ $this->currentCurrency = getCurrency();
+ $this->tax = Tax::getInstance($options);
+ $this->currency = Currency::getInstance($options);
+ }
+
+ foreach ($products as &$product) {
+ $product = $this->product($product, $options);
+ }
+
+ return $products;
+ }
+
+ function product(&$product, $options = []) {
+ $language = [];
+
+ if (! isset($this->currentCurrency)) {
+ $this->currentCurrency = getCurrency();
+ $this->tax = Tax::getInstance($options);
+ $this->currency = Currency::getInstance($options);
+ }
+
+ if ($product['language_id'] != $options['default_language_id']) {
+ $language = ['language' => $options['language']];
+
+ if (! $product['name']) {
+ $product['name'] = '[' . __('No translation') . ']';
+ $product['slug'] = 'no-translation';
+ }
+ }
+
+ if (isset($product['images'])) {
+ $product['images'] = json_decode($product['images'], true);
+
+ foreach ($product['images'] as &$image) {
+ $image['image'] = Images::image($image['image'], 'product', $options['image_size'] ?? 'medium');
+ }
+ }
+
+ if (isset($product['image']) && $product['image']) {
+ $product['image'] = Images::image($product['image'], 'product', $options['image_size'] ?? 'medium');
+ //$product['images'][] = ['image' => Images::image($product['image'], 'product')];
+ }
+
+ //rfc
+ $product['pubDate'] = date('r', strtotime($product['created_at']));
+ $product['modDate'] = date('r', strtotime($product['updated_at']));
+ $product['lastMod'] = date('Y-m-d\TH:i:sP', strtotime($product['updated_at']));
+
+ $url = ['slug' => $product['slug'], 'product_id' => $product['product_id']] + $language;
+ $product['url'] = url('product/product/index', $url);
+ $product['add_cart_url'] = url('cart/cart/add', ['product_id' => $product['product_id']]);
+ $product['buy_url'] = url('checkout/checkout/index', ['product_id' => $product['product_id']]);
+ $product['add_wishlist_url'] = url('user/wishlist/add', ['product_id' => $product['product_id']]);
+ $product['add_compare_url'] = url('cart/compare/add', ['product_id' => $product['product_id']]);
+ $product['full-url'] = url('product/product/index', $url + ['host' => SITE_URL, 'scheme' => $_SERVER['REQUEST_SCHEME'] ?? 'http']);
+
+ foreach (['price', 'old_price', 'min_price', 'max_price'] as $price) {
+ $amount = 0;
+
+ if (isset($product[$price]) && $product[$price]) {
+ $amount = $product[$price];
+ }
+ $product["{$price}_tax"] = $amount ? $this->tax->addTaxes($amount, $product['tax_type_id']) : 0;
+ $product["{$price}_formatted"] = $this->currency->format($amount);
+ $product["{$price}_tax_formatted"] = $this->currency->format($product["{$price}_tax"]);
+ $product["{$price}_price_currency"] = $this->currentCurrency;
+ }
+
+ $product['has_variants'] = false;
+
+ if (isset($product['min_price']) || isset($product['max_price'])) {
+ $product['has_variants'] = true;
+ }
+
+ return $product;
+ }
+}