Browse Source

Product variations in template, add select box

Vova Tkach 5 years ago
parent
commit
21e1f911d6

+ 10 - 1
assets/template/shop_product_html_file.go

@@ -38,7 +38,16 @@ var VarShopProductHtmlFile = []byte(`{{template "header.html" .}}
 						{{end}}
 					</div>
 					<div class="col-md-6">
-						<div class="card mt-3 mt-sm-3 mt-md-0 mt-lg-0">
+						{{if $.Data.Shop.Product.HaveVariations}}
+							<div class="card mt-3 mt-sm-3 mt-md-0 mt-lg-0">
+								<select class="form-control" onchange="document.location=this.value;">
+									{{range $variation := $.Data.Shop.Product.Variations}}
+										<option value="{{.Link}}"{{if .Selected}} selected{{end}}>{{.Name}}</option>
+									{{end}}
+								</select>
+							</div>
+						{{end}}
+						<div class="card mt-3{{if not $.Data.Shop.Product.HaveVariations}} mt-sm-3 mt-md-0 mt-lg-0{{end}}">
 							<div class="card-body">
 								<h3 class="price mb-0 mr-4">{{$.Data.Shop.Product.PriceFormat "%.2f"}} {{$.Data.Shop.Product.Currency.Code}}</h3><a href="" class="btn btn-success btn-buy">Buy</a>
 							</div>

+ 79 - 0
engine/fetdata/shop_product.go

@@ -9,6 +9,12 @@ import (
 	"golang-fave/utils"
 )
 
+type ShopProductVarItem struct {
+	Link     string
+	Name     string
+	Selected bool
+}
+
 type ShopProduct struct {
 	wrap   *wrapper.Wrapper
 	object *utils.MySql_shop_product
@@ -19,6 +25,7 @@ type ShopProduct struct {
 
 	images []*ShopProductImage
 	specs  []*ShopProductSpec
+	vars   []*ShopProductVarItem
 }
 
 func (this *ShopProduct) load() *ShopProduct {
@@ -124,6 +131,54 @@ func (this *ShopProduct) load() *ShopProduct {
 		}})
 	}
 
+	// Variations
+	if rows, err := this.wrap.DB.Query(
+		`SELECT
+			shop_products.id,
+			shop_products.name,
+			shop_products.alias
+		FROM
+			shop_products
+		WHERE
+			shop_products.active = 1 AND
+			(
+				(shop_products.id = ? OR shop_products.parent_id = ?) OR
+				(
+					(shop_products.id = ?) OR
+					(shop_products.parent_id IS NOT NULL AND shop_products.parent_id = ?)
+				)
+			)
+		ORDER BY
+			shop_products.name ASC
+		;`,
+		this.object.A_id,
+		this.object.A_id,
+		this.object.A_parent_id(),
+		this.object.A_parent_id(),
+	); err == nil {
+		defer rows.Close()
+		for rows.Next() {
+			var tmp_id int
+			var tmp_name string
+			var tmp_alias string
+			if err := rows.Scan(
+				&tmp_id,
+				&tmp_name,
+				&tmp_alias,
+			); *this.wrap.LogCpError(&err) == nil {
+				selected := false
+				if tmp_id == this.object.A_id {
+					selected = true
+				}
+				this.vars = append(this.vars, &ShopProductVarItem{
+					Link:     "/shop/" + tmp_alias + "/",
+					Name:     tmp_name,
+					Selected: selected,
+				})
+			}
+		}
+	}
+
 	return this
 }
 
@@ -317,3 +372,27 @@ func (this *ShopProduct) SpecsCount() int {
 	}
 	return len(this.specs)
 }
+
+func (this *ShopProduct) HaveVariations() bool {
+	if this == nil {
+		return false
+	}
+	if len(this.vars) <= 0 {
+		return false
+	}
+	return true
+}
+
+func (this *ShopProduct) Variations() []*ShopProductVarItem {
+	if this == nil {
+		return []*ShopProductVarItem{}
+	}
+	return this.vars
+}
+
+func (this *ShopProduct) VariationsCount() int {
+	if this == nil {
+		return 0
+	}
+	return len(this.vars)
+}

+ 10 - 1
hosts/localhost/template/shop-product.html

@@ -36,7 +36,16 @@
 						{{end}}
 					</div>
 					<div class="col-md-6">
-						<div class="card mt-3 mt-sm-3 mt-md-0 mt-lg-0">
+						{{if $.Data.Shop.Product.HaveVariations}}
+							<div class="card mt-3 mt-sm-3 mt-md-0 mt-lg-0">
+								<select class="form-control" onchange="document.location=this.value;">
+									{{range $variation := $.Data.Shop.Product.Variations}}
+										<option value="{{.Link}}"{{if .Selected}} selected{{end}}>{{.Name}}</option>
+									{{end}}
+								</select>
+							</div>
+						{{end}}
+						<div class="card mt-3{{if not $.Data.Shop.Product.HaveVariations}} mt-sm-3 mt-md-0 mt-lg-0{{end}}">
 							<div class="card-body">
 								<h3 class="price mb-0 mr-4">{{$.Data.Shop.Product.PriceFormat "%.2f"}} {{$.Data.Shop.Product.Currency.Code}}</h3><a href="" class="btn btn-success btn-buy">Buy</a>
 							</div>