Browse Source

Shop basket products counter as template variable

Vova Tkach 5 years ago
parent
commit
5fd14e30f1

+ 1 - 1
assets/template/header_html_file.go

@@ -50,7 +50,7 @@ var VarHeaderHtmlFile = []byte(`<!doctype html>
 								<a class="nav-link{{if eq $.Data.Module "404"}} active{{end}}" href="/not-existent-page/">404</a>
 							</li>
 							<li class="nav-item">
-								<a id="basket-nav-btn" class="nav-link" href="" onclick="window&&window.frontend&&frontend.ShopBasketOpen(this);return false;">Basket <span class="badge badge-pill badge-primary">0</span></a>
+								<a id="basket-nav-btn" class="nav-link" href="" onclick="window&&window.frontend&&frontend.ShopBasketOpen(this);return false;">Basket <span class="badge badge-pill badge-primary">{{$.Data.ShopBasketProductsCount}}</span></a>
 							</li>
 						</ul>
 					</div>

+ 1 - 1
assets/template/scripts_js_file.go

@@ -88,7 +88,7 @@ var VarScriptsJsFile = []byte(`(function(window, $) {
 			// Check if jQuery was loaded
 			if(typeof $ == 'function') {
 				ShopProductsInitLightGallery();
-				ShopBasketAjaxUpdateCount();
+				// ShopBasketAjaxUpdateCount();
 			} else {
 				console.log('Error: jQuery is not loaded!');
 			}

+ 15 - 0
engine/basket/basket.go

@@ -95,3 +95,18 @@ func (this *Basket) Remove(host, session_id string, db *sqlw.DB, product_id int)
 
 	return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
 }
+
+func (this *Basket) ProductsCount(host, session_id string) int {
+	if host != "" && session_id != "" {
+		this.Lock()
+		defer this.Unlock()
+
+		if h, ok := this.hosts[host]; ok == true {
+			if s, ok := h.sessions[session_id]; ok == true {
+				return s.ProductsCount()
+			}
+		}
+	}
+
+	return 0
+}

+ 4 - 0
engine/basket/session.go

@@ -80,6 +80,10 @@ func (this *session) Remove(db *sqlw.DB, product_id int) {
 	}
 }
 
+func (this *session) ProductsCount() int {
+	return this.TotalCount
+}
+
 func (this *session) updateTotals() {
 	this.TotalSum = 0
 	this.TotalCount = 0

+ 4 - 0
engine/fetdata/fetdata.go

@@ -197,3 +197,7 @@ func (this *FERData) CachedBlock5() template.HTML {
 	this.wrap.SetBlock5(data)
 	return data
 }
+
+func (this *FERData) ShopBasketProductsCount() int {
+	return this.wrap.ShopBasket.ProductsCount(this.wrap.CurrHost, this.wrap.GetSessionId())
+}

+ 1 - 1
hosts/localhost/template/header.html

@@ -48,7 +48,7 @@
 								<a class="nav-link{{if eq $.Data.Module "404"}} active{{end}}" href="/not-existent-page/">404</a>
 							</li>
 							<li class="nav-item">
-								<a id="basket-nav-btn" class="nav-link" href="" onclick="window&&window.frontend&&frontend.ShopBasketOpen(this);return false;">Basket <span class="badge badge-pill badge-primary">0</span></a>
+								<a id="basket-nav-btn" class="nav-link" href="" onclick="window&&window.frontend&&frontend.ShopBasketOpen(this);return false;">Basket <span class="badge badge-pill badge-primary">{{$.Data.ShopBasketProductsCount}}</span></a>
 							</li>
 						</ul>
 					</div>