Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Set product variant corresponding for each option combination

givanzed5414af3a64
diff --git a/app/component/product/options.php b/app/component/product/options.php
index f652a69..ac4734a 100644
--- a/app/component/product/options.php
+++ b/app/component/product/options.php
@@ -24,6 +24,8 @@ namespace Vvveb\Component\Product;
use \Vvveb\Sql\Product_Option_ValueSQL;
use \Vvveb\Sql\Product_OptionSQL;
+use \Vvveb\Sql\Product_VariantSQL;
+use \Vvveb\Sql\ProductSQL;
use Vvveb\System\Cart\Currency;
use Vvveb\System\Cart\Tax;
use Vvveb\System\Component\ComponentBase;
@@ -32,22 +34,52 @@ use Vvveb\System\Images;
class Options extends ComponentBase {
public static $defaultOptions = [
- 'start' => 0,
- 'limit' => NULL,
- 'site_id' => NULL,
- 'language_id' => NULL,
- 'product_id' => 'url',
- 'parent_id' => NULL,
- 'search' => NULL,
+ 'start' => 0,
+ 'limit' => NULL,
+ 'site_id' => NULL,
+ 'language_id' => NULL,
+ 'product_id' => 'url',
+ 'product_variant_id' => NULL,
+ 'parent_id' => NULL,
+ 'search' => NULL,
];
function results() {
+ $productSql = new ProductSQL();
$optionSql = new Product_OptionSQL();
$optionValuesSql = new Product_Option_ValueSQL();
+ $variantSql = new Product_VariantSQL();
+
+ $product = [];
+
+ if ($this->options['product_id']) {
+ $product = $productSql->get(['product_id' => $this->options['product_id']]);
+ }
$results = $optionSql->getAll($this->options);
$values = $optionValuesSql->getAll($this->options);
- $count = 0;
+
+ $voptions = ['product_id' => $this->options['product_id'], 'start' => 0, 'limit' => 1];
+
+ if ($this->options['product_variant_id']) {
+ $voptions['product_variant_id'] = [$this->options['product_variant_id']];
+ }
+
+ $variants = $variantSql->getAll($voptions);
+
+ $defaultVariant = [];
+ $defaultOptions = [];
+
+ if ($variants && isset($variants['product_variant'])) {
+ $defaultVariant = current($variants['product_variant']);
+ }
+
+ if ($defaultVariant) {
+ $json = '{' . preg_replace('/(\d+)/', '"\1"', $defaultVariant['options']) . '}';
+ $defaultOptions = json_decode($json, true);
+ }
+
+ $count = 0;
if ($results && isset($results['product_option'])) {
$options = &$results['product_option'];
@@ -63,8 +95,13 @@ class Options extends ComponentBase {
}
if ($value['price']) {
- $value['price_tax'] = $tax->addTaxes($value['price'], 1); //$product['tax_type_id']
- $value['price_formatted'] = $value['price_operator'] . $currency->format($value['price_tax']);
+ $value['price_tax'] = $tax->addTaxes($value['price'], $product['tax_type_id'] ?? 0);
+ $value['price_formatted'] = $value['price_operator'] . $currency->format($value['price_tax']);
+ }
+
+ if (isset($defaultOptions[$value['product_option_id']]) &&
+ $defaultOptions[$value['product_option_id']] == $value['product_option_value_id']) {
+ $value['checked'] = true;
}
if (isset($options[$value['product_option_id']])) {
diff --git a/app/template/components/product/options.tpl b/app/template/components/product/options.tpl
index 2f16e92..23222fd 100755
--- a/app/template/components/product/options.tpl
+++ b/app/template/components/product/options.tpl
@@ -70,6 +70,7 @@ if($options && is_array($options)) {
@value [data-v-value-input]|name = <?php echo 'option[' . $option['product_option_id'] . ']';?>
@value [data-v-value-input]|addNewAttribute = <?php if ($option['required']) echo 'required';?>
+ @value [data-v-value-input]|addNewAttribute = <?php if (isset($value['checked']) && $value['checked']) echo 'checked';?>
@value [data-v-value-input]|value = $value['product_option_value_id']
@value [data-v-value-price_formatted]|if_exists = $value['price']