Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Include produt variants component

givanza65ab53e63f5
diff --git a/app/component/product/variants.php b/app/component/product/variants.php
new file mode 100644
index 0000000..341cd19
--- /dev/null
+++ b/app/component/product/variants.php
@@ -0,0 +1,83 @@
+<?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\Component\Product;
+
+use \Vvveb\Sql\Product_VariantSQL;
+use \Vvveb\Sql\ProductSQL;
+use Vvveb\System\Cart\Currency;
+use Vvveb\System\Cart\Tax;
+use Vvveb\System\Component\ComponentBase;
+use Vvveb\System\Event;
+use Vvveb\System\Images;
+
+class Variants extends ComponentBase {
+ public static $defaultOptions = [
+ 'start' => 0,
+ 'limit' => NULL,
+ 'site_id' => NULL,
+ 'language_id' => NULL,
+ 'product_id' => 'url',
+ 'parent_id' => NULL,
+ 'search' => NULL,
+ ];
+
+ function results() {
+ $productSql = new ProductSQL();
+ $variantSql = new Product_VariantSQL();
+
+ $product = [];
+
+ if ($this->options['product_id']) {
+ $product = $productSql->get(['product_id' => $this->options['product_id']]);
+ }
+
+ $results = $variantSql->getAll($this->options);
+ $count = 0;
+
+ if ($results && isset($results['product_variant'])) {
+ $variants = &$results['product_variant'];
+ $tax = Tax::getInstance($this->options);
+ $currency = Currency::getInstance($this->options);
+
+ if ($variants) {
+ foreach ($variants as &$variant) {
+ $count++;
+
+ if ($variant['image']) {
+ $variant['image'] = Images::image($variant['image'], 'option', $this->options['image_size']);
+ }
+
+ if ($variant['price']) {
+ $variant['price_tax'] = $tax->addTaxes($variant['price'], $product['tax_type_id'] ?? 0);
+ $variant['price_formatted'] = $currency->format($variant['price_tax']);
+ }
+ }
+ }
+ }
+
+ $results['count'] = $count;
+ list($results) = Event :: trigger(__CLASS__,__FUNCTION__, $results);
+
+ return $results;
+ }
+}
diff --git a/app/template/components.tpl b/app/template/components.tpl
index 1e04ea8..c87b6f5 100755
--- a/app/template/components.tpl
+++ b/app/template/components.tpl
@@ -24,6 +24,7 @@ import(components/product/vendors.tpl, [data-v-component-product-vendors])
import(components/product/subscriptions.tpl, [data-v-component-product-subscriptions])
import(components/product/attributes.tpl, [data-v-component-product-attributes])
import(components/product/options.tpl, [data-v-component-product-options])
+import(components/product/variants.tpl, [data-v-component-product-variants])
import(components/content/archives.tpl, [data-v-component-content-archives])
import(components/currency.tpl, [data-v-component-currency])
import(components/language.tpl, [data-v-component-language])
diff --git a/app/template/components/product/variants.tpl b/app/template/components/product/variants.tpl
new file mode 100644
index 0000000..6451235
--- /dev/null
+++ b/app/template/components/product/variants.tpl
@@ -0,0 +1,87 @@
+@variants = [data-v-component-product-variants]
+@variant = [data-v-component-product-variants] [data-v-variant]
+@value = [data-v-component-product-variants] [data-v-variant] [data-v-value]
+
+@variant|deleteAllButFirstChild
+@value|deleteAllButFirstChild
+
+@variants|prepend = <?php
+$vvveb_is_page_edit = Vvveb\isEditor();
+if (isset($_variants_idx)) $_variants_idx++; else $_variants_idx = 0;
+$previous_component = isset($current_component)?$current_component:null;
+$product_variants = $current_component = $this->_component['product_variants'][$_variants_idx] ?? [];
+
+$variants = $product_variants['product_variant'] ?? [];
+
+$_pagination_count = $count = $product_variants['count'] ?? 0;
+$_pagination_limit = isset($variants['limit']) ? $variants['limit'] : 5;
+?>
+
+
+@variant|before = <?php
+$_default = (isset($vvveb_is_page_edit) && $vvveb_is_page_edit ) ? [
+ 1 => [
+ 'product_variant_id' => 1,
+ 'variant_id' => 1,
+ 'required' => 1,
+ 'type' => 'radio',
+ 'values' => [ 0 =>
+ [
+ 'product_variant_value_id' => 1,
+ 'product_variant_id' => 1,
+ 'product_id' => 1,
+ 'variant_id' => 1,
+ 'variant_value_id' => 1,
+ 'price' => 1,
+ 'image' => 'img',
+ ]
+ ]
+ ]
+] : false;
+
+$variants = empty($variants) ? $_default : $variants;
+
+if($variants && is_array($variants)) {
+ foreach ($variants as $index => $variant) {?>
+
+ @variant|data-variant_id = $variant['variant_id']
+
+ @variant|id = <?php echo 'variant-' . ($variant['product_variant_id'] ?? 0);?>
+
+ @variant [data-v-variant-content] = <?php echo($variant['content'] ?? '');?>
+
+ @variant img[data-v-variant-*]|src = $variant['@@__data-v-variant-(*)__@@']
+
+ @variant [data-v-variant-*]|innerText = $variant['@@__data-v-variant-(*)__@@']
+
+ @variant [data-v-variant-input]|value = $variant['value']
+ @variant [data-v-variant-input]|name = <?php echo 'variant[' . $variant['product_variant_id'] . ']';?>
+
+ @variant a[data-v-variant-*]|href = $variant['@@__data-v-variant-(*)__@@']
+
+ @value|before = <?php
+ if(isset($variant['values']) && is_array($variant['values'])) {
+ foreach ($variant['values'] as $vindex => $value) {?>
+
+ @variant variant[data-v-value] = $value['name']
+ @variant variant[data-v-value]|value = $value['product_variant_value_id']
+
+ @value [data-v-value-*]|innerText = $value['@@__data-v-value-(*)__@@']
+
+ @value [data-v-value-input]|name = <?php echo 'variant[' . $variant['product_variant_id'] . ']';?>
+ @value [data-v-value-input]|addNewAttribute = <?php if ($variant['required']) echo 'required';?>
+ @value [data-v-value-input]|value = $value['product_variant_value_id']
+
+ @value [data-v-value-price_formatted]|if_exists = $value['price']
+
+ @value img[data-v-value-*]|src = $value['@@__data-v-value-(*)__@@']
+
+ @value|after = <?php
+ }
+ }
+ ?>
+
+ @variant|after = <?php
+ }
+}
+?>