From: Vincent Vanwaelscappel Date: Mon, 21 Sep 2020 08:21:12 +0000 (+0200) Subject: done #3887 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=a8584506e2a364f22e6c6a07544ce1fe9bee8cc5;p=fluidbook-html5.git done #3887 @3 --- diff --git a/js/libs/fluidbook/cart/fluidbook.cart.mopec.js b/js/libs/fluidbook/cart/fluidbook.cart.mopec.js index fbb66a0c..f65bf77e 100644 --- a/js/libs/fluidbook/cart/fluidbook.cart.mopec.js +++ b/js/libs/fluidbook/cart/fluidbook.cart.mopec.js @@ -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 += ''; content += '' + item.product_name + '
' + $this.fluidbook.l10n.__('unit price') + ': ' + $this.formatPrice(unit); content += '
' + $this.fluidbook.l10n.__('price') + ': ' + $this.formatPrice(ht) + '
'; - content += ''; + var step = $this.minQuantities[item.product_sku]; + if (step === undefined) { + step = 1; + } + content += ''; content += '' + $this.formatPrice(unit) + ''; content += '' + $this.formatPrice(ht) + ''; content += '' + getSpriteIcon('interface-close') + '';