]> _ Git - fluidbook-html5.git/commitdiff
done #3887 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 21 Sep 2020 08:21:12 +0000 (10:21 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 21 Sep 2020 08:21:12 +0000 (10:21 +0200)
js/libs/fluidbook/cart/fluidbook.cart.mopec.js

index fbb66a0c45840691b69790d7d30d2c186ea593ed..f65bf77e85029da1cdcfde61decee082477f3ca0 100644 (file)
@@ -6,6 +6,8 @@ function FluidbookCartMopec(cart) {
     this.data;
     this.handleTooltips = true;
     this.handleTaxes = false;
+    this.minQuantities = {};
+    this.idBySku = {};
 
     this.initBaseURL();
     this.init();
@@ -36,9 +38,11 @@ FluidbookCartMopec.prototype = {
         if (this.dataok()) {
             this.updateCart();
         }
-        if (callback !== undefined) {
-            callback();
-        }
+        this.updateMinQuantities(function () {
+            if (callback !== undefined) {
+                callback();
+            }
+        });
     },
 
     init: function () {
@@ -70,9 +74,6 @@ FluidbookCartMopec.prototype = {
     addToCart: function (ref, quantity) {
         var $this = this;
         this.fluidbook.displayLoader();
-        if (quantity === undefined) {
-            quantity = 1;
-        }
 
         this.getProductIDBySKU(ref, function (product_id) {
             if (product_id === null) {
@@ -80,6 +81,11 @@ FluidbookCartMopec.prototype = {
                 $this.fluidbook.hideLoader();
                 $this.fluidbook.openInPopupIframe(url);
             } else {
+                var minQuantity = $this.minQuantities[ref];
+                if (quantity === undefined || isNaN(quantity) || quantity < minQuantity) {
+                    quantity = minQuantity
+                }
+  
                 $.ajax({
                     url: $this.baseURL + 'checkout/cart/add',
                     cache: false,
@@ -104,10 +110,48 @@ FluidbookCartMopec.prototype = {
         });
 
         return false;
+    },
+
+    getMinQuantity: function (sku, callback) {
+        if (this.minQuantities[sku] !== undefined) {
+            callback(this.minQuantities[sku]);
+            return;
+        }
+        var $this = this;
+        this.getProductIDBySKU(sku, function (product_id) {
+            if ($this.minQuantities[sku] === undefined) {
+                $this.minQuantities[sku] = 1;
+            }
+            callback($this.minQuantities[sku]);
+        });
+    },
 
+    updateMinQuantities: function (callback) {
+        var skus = [];
+        $.each(this.data.items, function (index, item) {
+            skus.push(item.product_sku);
+        });
+        this._updateMinQuantity(skus, callback);
+    },
+
+    _updateMinQuantity: function (list, callback) {
+        if (list.length === 0) {
+            callback();
+            return;
+        }
+        var sku = list.pop();
+        var $this = this;
+        this.getMinQuantity(sku, function () {
+            $this._updateMinQuantity(list, callback);
+        });
     },
 
     getProductIDBySKU: function (sku, callback) {
+        if (this.idBySku[sku] !== undefined) {
+            callback(this.idBySku[sku]);
+            return;
+        }
+        var $this = this;
         $.ajax({
             url: this.baseURL + 'fastorder/index/search',
             cache: false,
@@ -121,9 +165,16 @@ FluidbookCartMopec.prototype = {
                     product_id = null;
                 } else {
                     product_id = data[0].product_id;
+                    var min = 1;
+                    if (data[0].min !== undefined) {
+                        min = data[0].min;
+                    }
+                    $this.minQuantities[sku] = min;
                 }
+                $this.idBySku[sku] = product_id;
                 callback(product_id);
             }, error: function () {
+                $this.idBySku[sku] = null;
                 callback(null);
             }
         });
@@ -203,7 +254,11 @@ FluidbookCartMopec.prototype = {
             content += '<tr>';
             content += '<td class="name">' + item.product_name + '<div class="m">' + $this.fluidbook.l10n.__('unit price') + ': ' + $this.formatPrice(unit);
             content += '<br />' + $this.fluidbook.l10n.__('price') + ': ' + $this.formatPrice(ht) + '</div></td>';
-            content += '<td class="quantity"><input name="' + item.item_id + '" class="cartqty" type="number" min="0" max="10000" value="' + qty + '" step="1" /></td>';
+            var step = $this.minQuantities[item.product_sku];
+            if (step === undefined) {
+                step = 1;
+            }
+            content += '<td class="quantity"><input name="' + item.item_id + '" class="cartqty" type="number" step="' + step + '" min="0" max="10000" value="' + qty + '" /></td>';
             content += '<td class="price_unit">' + $this.formatPrice(unit) + '</td>';
             content += '<td class="price_excluding_taxes">' + $this.formatPrice(ht) + '</td>';
             content += '<td class="delete"><a href="#" data-cart-delete="' + item.item_id + '">' + getSpriteIcon('interface-close') + '</a></td>';