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 support

givanzfe2ed0d3e99e
diff --git a/admin/controller/product/product.php b/admin/controller/product/product.php
index e7bfbb3..4b56536 100755
--- a/admin/controller/product/product.php
+++ b/admin/controller/product/product.php
@@ -27,6 +27,7 @@ use Vvveb\Controller\Content\Edit;
use function Vvveb\dashesToCamelCase;
use Vvveb\Sql\Product_Option_ValueSQL;
use Vvveb\Sql\Product_OptionSQL;
+use Vvveb\Sql\Product_VariantSQL;
use Vvveb\Sql\ProductSQL;
class Product extends Edit {
@@ -40,6 +41,43 @@ class Product extends Edit {
protected $revisions = true;
+ function saveVariants() {
+ $post = &$this->request->post;
+ $post['product_variant'] = $post['product_variant'] ?? [];
+
+ if ($post['product_variant']) {
+ $deleteVariant = $this->request->post['delete']['product_variant_id'] ?? [];
+ $variantSql = new Product_VariantSQL();
+
+ unset($post['product_variant']['#'], $post['product_variant']['0']);
+
+ foreach ($post['product_variant'] as $i => $opt) {
+ $opt['product_id'] = $this->product_id;
+
+ if ($opt['product_variant_id']) {
+ //existing variant
+ $variantSql->edit([
+ 'product_variant' => $opt,
+ 'product_variant_id' => $opt['product_variant_id'],
+ ] + $this->global);
+ } else {
+ // new variant
+ unset($opt['product_variant_id']);
+ $result = $variantSql->add(['product_variant' => $opt] + $this->global);
+
+ if ($result && $result['product_variant']) {
+ //set new id to use for variant values
+ $opt['product_variant_id'] = $result['product_variant'];
+ }
+ }
+ }
+
+ if ($deleteVariant) {
+ $variantSql->delete(['product_variant_id' => $deleteVariant] + $this->global);
+ }
+ }
+ }
+
function saveOptions() {
$post = &$this->request->post;
$post['product_option'] = $post['product_option'] ?? [];
@@ -109,9 +147,10 @@ class Product extends Edit {
function save() {
$post = &$this->request->post;
- $post['requires_shipping'] = isset($post['requires_shipping']) ? 1 : 0;
- $post['manufacturer_id'] = isset($post['manufacturer_id']) ? ($post['manufacturer_id'] ?: 0) : null;
- $post['vendor_id'] = isset($post['vendor_id']) ? ($post['vendor_id'] ?: 0) : null;
+ $post['requires_shipping'] = isset($post['requires_shipping']) ? 1 : 0;
+ $post['subscription_onetime'] = isset($post['subscription_onetime']) ? 1 : 0;
+ $post['manufacturer_id'] = isset($post['manufacturer_id']) ? ($post['manufacturer_id'] ?: 0) : null;
+ $post['vendor_id'] = isset($post['vendor_id']) ? ($post['vendor_id'] ?: 0) : null;
parent::save();
@@ -119,9 +158,10 @@ class Product extends Edit {
if ($this->product_id) {
$this->saveOptions();
+ $this->saveVariants();
$product = new ProductSQL();
- $features = ['related', 'variant', 'subscription', 'discount', 'promotion', 'points', 'attribute', 'digital_asset'];
+ $features = ['related', /*'variant',*/ 'subscription', 'discount', 'promotion', 'points', 'attribute', 'digital_asset'];
foreach ($features as $feature) {
$feature = $this->object . '_' . $feature; //eg: product_related
@@ -138,6 +178,38 @@ class Product extends Edit {
return $this->index();
}
+ private function optionCombinations($arr) {
+ $names = [];
+ $values = [];
+
+ foreach ($arr as $optionId => $set) {
+ if (! $names) {
+ foreach ($set as $i => $val) {
+ $names[] = $val;
+ $values[] = $optionId . ':' . $i;
+ }
+ } else {
+ $length = count($names);
+ $newNames = [];
+ $newValues = [];
+
+ for ($l = 0; $l < $length; $l++) {
+ $currName = $names[$l];
+
+ foreach ($set as $j => $val) {
+ $newNames[] = $currName . ' / ' . $val;
+ $newValues[] = $values[$l] . ',' . $optionId . ':' . $j;
+ }
+ }
+
+ $names = $newNames;
+ $values = $newValues;
+ }
+ }
+
+ return [$names, $values];
+ }
+
function index() {
parent::index();
@@ -153,21 +225,32 @@ class Product extends Edit {
} else {
$data['option'] = [];
}
+ $optionIds = [];
if (isset($view->product['product_option'])) {
$product_option_value = &$view->product['product_option_value'];
$product_option = &$view->product['product_option'];
- foreach ($product_option_value as &$value) {
- if (isset($product_option[$value['product_option_id']])) {
- $product_option[$value['product_option_id']]['values'][$value['product_option_value_id']] = $value;
- }
- }
-
$option_value_content = [];
+ $names = [];
+
+ $default = '';
+
+ if ($view->product['language_id'] != $this->global['default_language_id']) {
+ $default = '[' . __('No translation') . ']';
+ }
foreach ($view->product['option_value_content'] as &$value) {
$option_value_content[$value['option_id']][$value['option_value_id']] = $value;
+ $names[$value['option_value_id']] = $value['name'] ?? $default;
+ //$optionIds[$value['product_option_id']][$value['product_option_value_id']] = $value['name'];
+ }
+
+ foreach ($product_option_value as &$value) {
+ if (isset($product_option[$value['product_option_id']])) {
+ $product_option[$value['product_option_id']]['values'][$value['product_option_value_id']] = $value;
+ $optionIds[$value['product_option_id']][$value['product_option_value_id']] = $names[$value['option_value_id']] ?? $default;
+ }
}
$view->product['option_value_content'] = $option_value_content;
@@ -175,6 +258,13 @@ class Product extends Edit {
$view->product['product_option'] = [];
}
+ $view->combinations = [];
+
+ if ($optionIds) {
+ $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'] : '');
$view->product['vendor_id'] = (($view->product['vendor_id'] ?? 0) ? $view->product['vendor_id'] : '');
$view->product['status'] = $view->product['status'] ?? 1;
diff --git a/admin/sql/mysqli/product_variant.sql b/admin/sql/mysqli/product_variant.sql
new file mode 100644
index 0000000..76b6979
--- /dev/null
+++ b/admin/sql/mysqli/product_variant.sql
@@ -0,0 +1,129 @@
+-- product_variant
+
+ -- get all product_variant
+
+ CREATE PROCEDURE getAll(
+ IN product_id INT,
+ IN min_stock_quantity INT,
+ IN product_variant_id ARRAY,
+
+ -- pagination
+ IN start INT,
+ IN limit INT,
+ IN order_by CHAR,
+ IN direction CHAR,
+
+ OUT fetch_all,
+ )
+ BEGIN
+
+ SELECT *, options as array_key
+ FROM product_variant
+ WHERE 1 = 1
+
+ @IF !empty(:product_id)
+ THEN
+ AND product_id = :product_id
+ END @IF
+
+ @IF !empty(:min_stock_quantity)
+ THEN
+ AND stock_quantity > :min_stock_quantity
+ END @IF
+
+ @IF !empty(:product_variant_id)
+ THEN
+ AND product_variant_id IN (:product_variant_id)
+ END @IF
+
+
+ -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
+ @IF isset(:order_by)
+ THEN
+ ORDER BY product_variant.$order_by $direction
+ @ELSE
+ ORDER BY product_variant.product_variant_id ASC
+ END @IF
+
+
+ @IF isset(:limit)
+ THEN
+ @SQL_LIMIT(:start, :limit)
+ END @IF;
+
+ END
+
+ -- get one product_variant
+
+ CREATE PROCEDURE get(
+ IN product_id INT,
+ IN product_variant_id INT,
+
+ OUT fetch_row,
+ )
+ BEGIN
+
+ SELECT *
+ FROM product_varian
+ WHERE product_variant_id = :product_variant_id LIMIT 1;
+
+ END
+
+ CREATE PROCEDURE add(
+ IN product_id INT,
+ IN product_variant ARRAY,
+
+ OUT insert_id
+ )
+ BEGIN
+ -- allow only table fields and set defaults for missing values
+ :product_variant = @FILTER(:product_variant, product_variant)
+
+ INSERT INTO product_variant
+ ( @KEYS(:product_variant) )
+
+ VALUES (:product_variant );
+
+ END
+
+ CREATE PROCEDURE edit(
+ IN product_variant ARRAY,
+ IN product_variant_id INT,
+ OUT affected_rows
+ )
+ BEGIN
+ -- allow only table fields and set defaults for missing values
+ :product_variant = @FILTER(:product_variant, product_variant)
+
+ UPDATE product_variant
+
+ SET @LIST(:product_variant)
+
+ WHERE product_variant_id = :product_variant_id
+
+ END
+
+ CREATE PROCEDURE delete(
+ IN product_id INT,
+ IN product_variant_id ARRAY,
+
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM
+ product_variant
+ WHERE 1 = 1
+
+ @IF !empty(:product_variant_id)
+ THEN
+ AND product_variant_id IN (:product_variant_id)
+ END @IF
+
+ @IF !empty(:product_id)
+ THEN
+ AND product_id = :product_id
+ END @IF
+ ;
+
+ END
diff --git a/admin/sql/pgsql/product_variant.sql b/admin/sql/pgsql/product_variant.sql
new file mode 100644
index 0000000..d3b8db5
--- /dev/null
+++ b/admin/sql/pgsql/product_variant.sql
@@ -0,0 +1,129 @@
+-- product_variant
+
+ -- get all product_variant
+
+ CREATE PROCEDURE getAll(
+ IN product_id INT,
+ IN min_stock_quantity INT,
+ IN product_variant_id ARRAY,
+
+ -- pagination
+ IN start INT,
+ IN limit INT,
+ IN order_by CHAR,
+ IN direction CHAR,
+
+ OUT fetch_all,
+ )
+ BEGIN
+
+ SELECT *, options as array_key
+ FROM product_variant
+ WHERE 1 = 1
+
+ @IF !empty(:product_id)
+ THEN
+ AND product_id = :product_id
+ END @IF
+
+ @IF !empty(:min_stock_quantity)
+ THEN
+ AND stock_quantity > :min_stock_quantity
+ END @IF
+
+ @IF !empty(:product_variant_id)
+ THEN
+ AND product_variant_id IN (:product_variant_id)
+ END @IF
+
+
+ -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
+ @IF isset(:order_by)
+ THEN
+ ORDER BY product_variant.$order_by $direction
+ @ELSE
+ ORDER BY product_variant.product_variant_id ASC
+ END @IF
+
+
+ @IF isset(:limit)
+ THEN
+ @SQL_LIMIT(:start, :limit)
+ END @IF;
+
+ END
+
+ -- get one product_variant
+
+ CREATE PROCEDURE get(
+ IN product_id INT,
+ IN product_variant_id INT,
+
+ OUT fetch_row,
+ )
+ BEGIN
+
+ SELECT *
+ FROM product_varian
+ WHERE product_variant_id = :product_variant_id LIMIT 1;
+
+ END
+
+ CREATE PROCEDURE add(
+ IN product_id INT,
+ IN product_variant ARRAY,
+
+ OUT insert_id
+ )
+ BEGIN
+ -- allow only table fields and set defaults for missing values
+ :product_variant = @FILTER(:product_variant, product_variant)
+
+ INSERT INTO product_variant
+ ( @KEYS(:product_variant) )
+
+ VALUES (:product_variant );
+
+ END
+
+ CREATE PROCEDURE edit(
+ IN product_variant ARRAY,
+ IN product_variant_id INT,
+ OUT affected_rows
+ )
+ BEGIN
+ -- allow only table fields and set defaults for missing values
+ :product_variant = @FILTER(:product_variant, product_variant)
+
+ UPDATE product_variant
+
+ SET @LIST(:product_variant)
+
+ WHERE product_variant_id = :product_variant_id
+
+ END
+
+ CREATE PROCEDURE delete(
+ IN product_id INT,
+ IN product_variant_id ARRAY,
+
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM
+ product_variant
+ WHERE 1 = 1
+
+ @IF !empty(:product_variant_id)
+ THEN
+ AND product_variant_id IN (:product_variant_id)
+ END @IF
+
+ @IF !empty(:product_id)
+ THEN
+ AND product_id = :product_id
+ END @IF
+ ;
+
+ END
diff --git a/admin/sql/sqlite/product_variant.sql b/admin/sql/sqlite/product_variant.sql
new file mode 100644
index 0000000..3762e27
--- /dev/null
+++ b/admin/sql/sqlite/product_variant.sql
@@ -0,0 +1,129 @@
+-- product_variant
+
+ -- get all product_variant
+
+ CREATE PROCEDURE getAll(
+ IN product_id INT,
+ IN min_stock_quantity INT,
+ IN product_variant_id ARRAY,
+
+ -- pagination
+ IN start INT,
+ IN limit INT,
+ IN order_by CHAR,
+ IN direction CHAR,
+
+ OUT fetch_all,
+ )
+ BEGIN
+
+ SELECT *, options as array_key
+ FROM product_variant
+ WHERE 1 = 1
+
+ @IF !empty(:product_id)
+ THEN
+ AND product_id = :product_id
+ END @IF
+
+ @IF !empty(:min_stock_quantity)
+ THEN
+ AND stock_quantity > :min_stock_quantity
+ END @IF
+
+ @IF !empty(:product_variant_id)
+ THEN
+ AND product_variant_id IN (:product_variant_id)
+ END @IF
+
+
+ -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
+ @IF isset(:order_by)
+ THEN
+ ORDER BY product_variant.$order_by $direction
+ @ELSE
+ ORDER BY product_variant.product_variant_id ASC
+ END @IF
+
+
+ @IF isset(:limit)
+ THEN
+ @SQL_LIMIT(:start, :limit)
+ END @IF;
+
+ END
+
+ -- get one product_variant
+
+ CREATE PROCEDURE get(
+ IN product_id INT,
+ IN product_variant_id INT,
+
+ OUT fetch_row,
+ )
+ BEGIN
+
+ SELECT *
+ FROM product_varian
+ WHERE product_variant_id = :product_variant_id LIMIT 1;
+
+ END
+
+ CREATE PROCEDURE add(
+ IN product_id INT,
+ IN product_variant ARRAY,
+
+ OUT insert_id
+ )
+ BEGIN
+ -- allow only table fields and set defaults for missing values
+ :product_variant = @FILTER(:product_variant, product_variant)
+
+ INSERT INTO product_variant
+ ( @KEYS(:product_variant) )
+
+ VALUES (:product_variant );
+
+ END
+
+ CREATE PROCEDURE edit(
+ IN product_variant ARRAY,
+ IN product_variant_id INT,
+ OUT affected_rows
+ )
+ BEGIN
+ -- allow only table fields and set defaults for missing values
+ :product_variant = @FILTER(:product_variant, product_variant)
+
+ UPDATE product_variant
+
+ SET @LIST(:product_variant)
+
+ WHERE product_variant_id = :product_variant_id
+
+ END
+
+ CREATE PROCEDURE delete(
+ IN product_id INT,
+ IN product_variant_id ARRAY,
+
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM
+ product_variant
+ WHERE 1 = 1
+
+ @IF !empty(:product_variant_id)
+ THEN
+ AND product_variant_id IN (:product_variant_id)
+ END @IF
+
+ @IF !empty(:product_id)
+ THEN
+ AND product_id = :product_id
+ END @IF
+ ;
+
+ END
diff --git a/install/sql/insert/product/product_variant.sql b/install/sql/insert/product/product_variant.sql
index 6f24a74..4b88e21 100644
--- a/install/sql/insert/product/product_variant.sql
+++ b/install/sql/insert/product/product_variant.sql
@@ -1,3 +1,27 @@
-INSERT INTO `product_variant` (`product_id`, `product_variant_id`) VALUES
-(18, 14),
-(18, 15);
\ No newline at end of file
+INSERT INTO `product_variant` (`product_variant_id`, `product_id`, `options`, `image`, `price`, `stock_quantity`, `weight`, `sku`, `barcode`) VALUES
+(1, 19, '1:1,2:4,3:8', '', 1000.0000, 100, 0.00000000, '', ''),
+(2, 19, '1:1,2:4,3:9', '', 1100.0000, 100, 0.00000000, '', ''),
+(3, 19, '1:1,2:5,3:7', '', 1200.0000, 100, 0.00000000, '', ''),
+(4, 19, '1:1,2:5,3:8', '', 1300.0000, 0, 0.00000000, '', ''),
+(5, 19, '1:1,2:5,3:9', '', 1400.0000, 100, 0.00000000, '', ''),
+(6, 19, '1:1,2:6,3:7', '', 1500.0000, 100, 0.00000000, '', ''),
+(7, 19, '1:1,2:6,3:8', '', 1600.0000, 100, 0.00000000, '', ''),
+(8, 19, '1:1,2:6,3:9', '', 1700.0000, 100, 0.00000000, '', ''),
+(9, 19, '1:2,2:4,3:7', '', 1800.0000, 100, 0.00000000, '', ''),
+(10, 19, '1:2,2:4,3:8', '', 1900.0000, 100, 0.00000000, '', ''),
+(11, 19, '1:2,2:4,3:9', '', 2000.0000, 100, 0.00000000, '', ''),
+(12, 19, '1:2,2:5,3:7', '', 2100.0000, 0, 0.00000000, '', ''),
+(13, 19, '1:2,2:5,3:8', '', 2200.0000, 100, 0.00000000, '', ''),
+(14, 19, '1:2,2:5,3:9', '', 2300.0000, 100, 0.00000000, '', ''),
+(15, 19, '1:2,2:6,3:7', '', 2400.0000, 100, 0.00000000, '', ''),
+(16, 19, '1:2,2:6,3:8', '', 2500.0000, 100, 0.00000000, '', ''),
+(17, 19, '1:2,2:6,3:9', '', 2600.0000, 100, 0.00000000, '', ''),
+(18, 19, '1:3,2:4,3:7', '', 2700.0000, 100, 0.00000000, '', ''),
+(19, 19, '1:3,2:4,3:8', '', 2800.0000, 100, 0.00000000, '', ''),
+(20, 19, '1:3,2:4,3:9', '', 2900.0000, 100, 0.00000000, '', ''),
+(21, 19, '1:3,2:5,3:7', '', 3000.0000, 100, 0.00000000, '', ''),
+(22, 19, '1:3,2:5,3:8', '', 3100.0000, 100, 0.00000000, '', ''),
+(23, 19, '1:3,2:5,3:9', '', 3200.0000, 100, 0.00000000, '', ''),
+(24, 19, '1:3,2:6,3:7', '', 3300.0000, 100, 0.00000000, '', ''),
+(25, 19, '1:3,2:6,3:8', '', 3400.0000, 100, 0.00000000, '', ''),
+(26, 19, '1:3,2:6,3:9', '', 3500.0000, 100, 0.00000000, '', '');
\ No newline at end of file
diff --git a/install/sql/mysqli/schema/product/product_variant.sql b/install/sql/mysqli/schema/product/product_variant.sql
index ddbe826..278713c 100755
--- a/install/sql/mysqli/schema/product/product_variant.sql
+++ b/install/sql/mysqli/schema/product/product_variant.sql
@@ -1,7 +1,17 @@
DROP TABLE IF EXISTS `product_variant`;
CREATE TABLE `product_variant` (
+ `product_variant_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`product_id` INT UNSIGNED NOT NULL,
- `product_variant_id` INT UNSIGNED NOT NULL,
- PRIMARY KEY (`product_id`,`product_variant_id`)
+ `options` varchar(191) NOT NULL DEFAULT '',
+ `image` varchar(191) NOT NULL DEFAULT '',
+ `price` decimal(15,4) NOT NULL DEFAULT 0.0000,
+ `old_price` decimal(15,4) NOT NULL DEFAULT 0.0000,
+ `stock_quantity` int(4) NOT NULL DEFAULT '0',
+ `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
+ `sku` varchar(64) NOT NULL DEFAULT '',
+ `barcode` varchar(64) NOT NULL DEFAULT '',
+ PRIMARY KEY (`product_variant_id`),
+ KEY (`product_id`, `stock_quantity`, `options`),
+ KEY (`options`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
diff --git a/install/sql/pgsql/schema/product/product_variant.sql b/install/sql/pgsql/schema/product/product_variant.sql
index 07c183c..90c5460 100755
--- a/install/sql/pgsql/schema/product/product_variant.sql
+++ b/install/sql/pgsql/schema/product/product_variant.sql
@@ -1,6 +1,20 @@
DROP TABLE IF EXISTS product_variant;
+
+DROP SEQUENCE IF EXISTS product_variant_seq;
+CREATE SEQUENCE product_variant_seq;
+
CREATE TABLE product_variant (
+ "product_variant_id" int check ("product_variant_id" > 0) NOT NULL DEFAULT NEXTVAL ('product_variant_seq'),
"product_id" int check ("product_id" > 0) NOT NULL,
- "product_variant_id" int check ("product_variant_id" > 0) NOT NULL,
- PRIMARY KEY ("product_id","product_variant_id")
+ "options" varchar(191) NOT NULL DEFAULT '',
+ "image" varchar(191) NOT NULL DEFAULT '',
+ "price" decimal(15,4) NOT NULL DEFAULT 0.0000,
+ "old_price" decimal(15,4) NOT NULL DEFAULT 0.0000,
+ "stock_quantity" int NOT NULL DEFAULT 0,
+ "weight" decimal(15,8) NOT NULL DEFAULT '0.00000000',
+ "sku" varchar(64) NOT NULL DEFAULT '',
+ "barcode" varchar(64) NOT NULL DEFAULT '',
+ PRIMARY KEY ("product_variant_id")
);
+
+CREATE INDEX "product_variant_product_id_product_option" ON "product_variant" ("product_id","product_variant_id", "options");
diff --git a/install/sql/sqlite/schema/product/product_variant.sql b/install/sql/sqlite/schema/product/product_variant.sql
index c2c101f..96cd51a 100755
--- a/install/sql/sqlite/schema/product/product_variant.sql
+++ b/install/sql/sqlite/schema/product/product_variant.sql
@@ -1,7 +1,18 @@
DROP TABLE IF EXISTS `product_variant`;
CREATE TABLE `product_variant` (
-`product_id` INT NOT NULL,
-`product_variant_id` INT NOT NULL,
-PRIMARY KEY (`product_id`,`product_variant_id`)
+ `product_variant_id` INTEGER PRIMARY KEY AUTOINCREMENT,
+ `product_id` INT UNSIGNED NOT NULL,
+ `options`TEXT NOT NULL DEFAULT '',
+ `image` TEXT NOT NULL DEFAULT '',
+ `price` decimal(15,4) NOT NULL DEFAULT 0.0000,
+ `old_price` decimal(15,4) NOT NULL DEFAULT 0.0000,
+ `stock_quantity` int(4) NOT NULL DEFAULT '0',
+ `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
+ `sku` varchar(64) NOT NULL DEFAULT '',
+ `barcode` varchar(64) NOT NULL DEFAULT ''
+-- PRIMARY KEY (`product_variant_id`),
+-- KEY (`product_id`,`product_variant_id`, `product_option`)
);
+
+CREATE INDEX `product_variant_product_id_product_option` ON `product_variant` (`product_id`,`product_variant_id`, `options`);
\ No newline at end of file