Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Product variants generate only for selected product options

givanz54c1b1a026f2
diff --git a/admin/controller/product/product.php b/admin/controller/product/product.php
index 4b56536..5f59070 100755
--- a/admin/controller/product/product.php
+++ b/admin/controller/product/product.php
@@ -260,9 +260,26 @@ class Product extends Edit {
$view->combinations = [];
- if ($optionIds) {
- $combinations = $this->optionCombinations($optionIds);
- $view->combinations = array_combine($combinations[1], $combinations[0]);
+ if ($optionIds && $view->product['product_variant']) {
+ foreach ($view->product['product_variant'] as $opts => &$variant) {
+ $keys = explode(',', $opts);
+ $name = '';
+ foreach ($keys as $key) {
+ $r = explode(':', $key);
+ $o = $r[0];
+ $v = $r[1];
+
+ if ($name) {
+ $name .= ' / ';
+ }
+
+ $name .= $optionIds[$o][$v];
+ }
+
+ $variant['name'] = $name;
+ }
+ //$combinations = $this->optionCombinations($optionIds);
+ //$view->combinations = array_combine($combinations[1], $combinations[0]);
}
$view->product['manufacturer_id'] = (($view->product['manufacturer_id'] ?? 0) ? $view->product['manufacturer_id'] : '');
diff --git a/admin/template/product/product/variant.tpl b/admin/template/product/product/variant.tpl
old mode 100755
new mode 100644
index 66acaf3..59afcfb
--- a/admin/template/product/product/variant.tpl
+++ b/admin/template/product/product/variant.tpl
@@ -8,10 +8,11 @@ foreach ($this->product['product_variant'] as $options => $variant) {
$product_variant_id = $variant['product_variant_id'];
?>
@product-variant|data-id = $variant['product_variant_id']
- @product-variant [data-v-variant-name] = $this->combinations[$options]
+ @product-variant [data-v-variant-name] = $variant['name']
@product-variant input[data-v-variant-*]|value = $variant['@@__data-v-variant-(*)__@@']
@product-variant input[data-v-variant-*]|name = <?php echo "product_variant[$product_variant_id][@@__data-v-variant-(*)__@@]";?>
+ @product-variant [data-v-variant-combination]|value = $options
@product-variant [data-v-variant-combination]|before = <?php
$name = 'combinations';
diff --git a/app/template/components/product/options.tpl b/app/template/components/product/options.tpl
old mode 100755
new mode 100644
index 23222fd..712078d
--- a/app/template/components/product/options.tpl
+++ b/app/template/components/product/options.tpl
@@ -56,6 +56,9 @@ if($options && is_array($options)) {
@option [data-v-option-input]|value = $option['value']
@option [data-v-option-input]|name = <?php echo 'option[' . $option['product_option_id'] . ']';?>
+
+ @option [data-v-option-input]|addNewAttribute = <?php if ($option['required']) echo 'required';?>
+ @option [data-v-option-name]|addClass = <?php if ($option['required']) echo 'required';?>
@option a[data-v-option-*]|href = $option['@@__data-v-option-(*)__@@']
@@ -63,8 +66,8 @@ if($options && is_array($options)) {
if(isset($option['values']) && is_array($option['values'])) {
foreach ($option['values'] as $vindex => $value) {?>
- @option option[data-v-value] = $value['name']
- @option option[data-v-value]|value = $value['product_option_value_id']
+ @option option[data-v-value]|innerText = $value['name']
+ @option option[data-v-value]|value = $value['product_option_value_id']
@value [data-v-value-*]|innerText = $value['@@__data-v-value-(*)__@@']
diff --git a/public/js/app/theme.js b/public/js/app/theme.js
index 2474fcd..cbf6073 100644
--- a/public/js/app/theme.js
+++ b/public/js/app/theme.js
@@ -179,28 +179,52 @@ function productPage() {
let variant;
if (Object.keys(productVariants).length > 0) {// have variants
- let variantId = JSON.stringify(selectedProductOptions).replaceAll(/[^\d:,]+/g,'');
+ //let variantId = JSON.stringify(selectedProductOptions).replaceAll(/[^\d:,]+/g,'');
+ //variant = productVariants[variantId];
+ variant = false;
+ let variantId = "";
+ let options = {};
+ let opts = [];
+ let lastVariantId = "";
+ for (opt in selectedProductOptions) {
+ if (variantId) {
+ variantId += ","
+ }
+
+ variantId += opt + ":" + selectedProductOptions[opt];
+ opts.push(opt);
+
+ if (productVariants[variantId]) {
+ options[variantId] = Array.from(opts);
+ lastVariantId = variantId;
+ }
+ }
- variant = productVariants[variantId];
+ variant = productVariants[lastVariantId];
if (variant) {
isStock = variant && variant.stock_quantity > 0 ? true : false;
productOptionsContainer.querySelectorAll('input[type="radio"]').forEach(radio => {
- const currentOptions = Object.assign({}, selectedProductOptions);
let id = radio.name.match(/(\d+)/)[1] ?? false;
- if (id) {
+ const currentOptions = Object.assign({}, selectedProductOptions);
+ if (!options[lastVariantId].includes(id)) return;
+
+ if (id && currentOptions[id]) {
currentOptions[id] = radio.value;
let currentVariantId = JSON.stringify(currentOptions).replaceAll(/[^\d:,]+/g,'');
let variant = productVariants[currentVariantId];
- let hasStock = variant && variant.stock_quantity > 0 ? true : false;
- let text = radio.parentNode.querySelector("[data-v-value-name]");
+
+ if (variant) {
+ let hasStock = variant && variant.stock_quantity > 0 ? true : false;
+ let text = radio.parentNode.querySelector("[data-v-value-name]");
- if (hasStock) {
- text.style.textDecoration = "";
- text.style.opacity = "";
- } else {
- text.style.textDecoration = "line-through";
- text.style.opacity = 0.5;
+ if (hasStock) {
+ text.style.textDecoration = "";
+ text.style.opacity = "";
+ } else {
+ text.style.textDecoration = "line-through";
+ text.style.opacity = 0.5;
+ }
}
}
});
@@ -236,7 +260,7 @@ if ('serviceWorker' in navigator) {
}
function togglePasswordInput(element, input) {
- let password = document.getElementById(input);
+ let password = document.getElementById(input) || element.parentNode.querySelector("[type=text],[type=password]");
if (password.type == "password") {
password.type = "text";
let i = element.querySelector("i")