var $this = this;
this.cart = cart;
this.fluidbook = this.cart.fluidbook;
- this.items = this.cart.getSavedData();
- this.data;
- this.loggedIn = false;
- this.handleTooltips = true;
- this.handleTaxes = false;
- this.minQuantities = {};
- this.idBySku = {};
-
- this.initBaseURL();
+ this.data = this.fluidbook.settings.basketReferences;
this.init();
}
FluidbookCartPuma.prototype = {
- initBaseURL: function () {
- this.baseURL = window.location.protocol + '//' + window.location.host + '/' + this.fluidbook.l10n.currentLang + '/';
- },
- updateCartData: function (callback) {
- var $this = this;
- this.sendCookie();
-
- $.ajax({
- url: this.baseURL + "customer/section/load/",
- dataType: 'json',
- cache: false,
- xhrFields: {withCredentials: true},
- success: function (data) {
- $this.fluidbook.hideLoader();
- $this.updateCartFromData(data.cart, data.customer, callback);
- },
- });
- },
-
- updateCartFromData: function (data, customer, callback) {
- this.data = data;
- this.loggedIn = customer.websiteId !== undefined && customer.websiteId !== null;
- if (this.dataok()) {
- this.updateCart();
- }
- this.updateMinQuantities(function () {
- if (callback !== undefined) {
- callback();
- }
- });
- },
-
init: function () {
- var $this = this;
-
- $(document).on('change', '[data-menu="cart"] .input-number', function () {
- $this.updateCartFromQuantities(this);
- return true;
- });
-
- this.updateCartData(function () {
-
- });
-
- setTimeout(function () {
- $this.updateCartData(function () {
-
- });
- }, 60000);
-
- $(window).on('focus', function () {
- $this.fluidbook.menu.closeView(function () {
- }, true, false);
- $this.updateCartData(function () {
-
- });
- });
-
-
- },
-
- checkLoginSession: function () {
- if (!this.loggedIn) {
- this.fluidbook.links.triggerLinkById('login');
- return false;
- }
- return true;
+ this.items = this.fluidbook.cache.get('cart', []);
},
addToCart: function (ref, quantity) {
- var $this = this;
- if (!this.checkLoginSession()) {
- return;
- }
- this.fluidbook.displayLoader();
-
- this.getProductIDBySKU(ref, function (product_id) {
- if (product_id === null) {
- var url = this.fluidbook.settings.product_zoom_references[ref][0];
- $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,
- data: {product: product_id, qty: quantity, form_key: $this.getFormKey()},
- method: 'post',
- xhrFields: {withCredentials: true},
- dataType: 'json',
- success: function (data) {
- if (data.backUrl) {
- $this.fluidbook.hideLoader();
- $this.fluidbook.openInPopupIframe(data.backUrl);
- } else {
- $this.fluidbook.tooltip.displayTooltip($this.fluidbook.l10n.__("the item has been added to your cart"), 'invert');
- $this.fluidbook.hideLoader();
- $this.updateCartData(function () {
- $this.fluidbook.menu.openView('cart');
- });
- }
- }
- });
- }
- });
-
- return false;
- },
-
- getMinQuantity: function (sku, callback) {
- if (this.minQuantities[sku] !== undefined) {
- callback(this.minQuantities[sku]);
- return;
+ if (this.items.indexOf(ref) === -1) {
+ this.items.push(ref);
+ this.save();
}
- var $this = this;
- console.log('check qty for ' + sku);
- this.getProductIDBySKU(sku, function (product_id) {
- if ($this.minQuantities[sku] === undefined) {
- console.log('no qty for ' + sku);
- var e = sku.split('.');
- if (e.length > 1) {
- e.pop();
- var nsku = e.join('.') + '.';
- console.log('check qty for ' + nsku);
- $this.getProductIDBySKU(nsku, function (product_id) {
- if ($this.minQuantities[nsku] === undefined) {
- console.log('no qty for ' + nsku);
- var ee = nsku.split('.');
- if (ee.length > 1) {
- ee.pop();
- ee.pop();
- var nnsku = ee.join('.') + '.';
- console.log('check qty for ' + nnsku);
- $this.getProductIDBySKU(nnsku, function (product_id) {
- console.log('no qty for ' + nnsku);
- if ($this.minQuantities[nnsku] === undefined) {
- $this.minQuantities[nnsku] = 1;
- }
- $this.minQuantities[nsku] = $this.minQuantities[nnsku];
- $this.minQuantities[sku] = $this.minQuantities[nnsku];
- callback($this.minQuantities[nnsku]);
- });
- return;
- }
- } else {
- $this.minQuantities[sku] = $this.minQuantities[nsku];
- }
- callback($this.minQuantities[nsku]);
- });
- return;
- }
- } else {
- callback($this.minQuantities[sku]);
- }
- });
+ return true;
},
- updateMinQuantities: function (callback) {
- var skus = [];
- $.each(this.data.items, function (index, item) {
- skus.push(item.product_sku);
- });
- this._updateMinQuantity(skus, callback);
+ removeFromCart: function (key) {
+ this.items.splice(key, 1);
+ this.save();
},
- _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);
- });
+ save: function () {
+ this.fluidbook.cache.set('cart', this.items);
},
-
- 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,
- data: {product: sku, sort_order: 0},
- method: 'post',
- xhrFields: {withCredentials: true},
- dataType: 'json',
- success: function (data) {
- var product_id;
- if (data === null || data === undefined || data === '' || data.length === 0 || !data) {
- product_id = null;
- } else {
- product_id = data[0].product_id;
- var min = 1;
- if (data[0].min !== undefined) {
- min = data[0].min;
- }
- console.log('set min qty for ' + sku + ' : ' + min)
- $this.minQuantities[sku] = min;
- }
- $this.idBySku[sku] = product_id;
- callback(product_id);
- }, error: function () {
- $this.idBySku[sku] = null;
- callback(null);
- }
- });
+ getItemsNumbers: function () {
+ return this.items.length;
},
-
- sendCookie: function () {
+ getAllQuantities: function () {
+ return this.getItemsNumbers();
},
- removeFromCart: function (item_id, callback) {
- var $this = this;
- this.fluidbook.displayLoader();
- $.ajax({
- url: $this.baseURL + 'checkout/sidebar/removeItem',
- method: 'post',
- data: {item_id: item_id, form_key: this.getFormKey()},
- xhrFields: {withCredentials: true},
- success: function () {
- $this.updateCartData(callback);
- }
- })
- },
- getItemsNumbers: function () {
- if (!this.dataok()) {
- return 0;
+ updateCart: function () {
+ if ($('#pumacart').length > 0) {
+ $('#pumacart .content').html(this.getCartContent());
}
- return this.data.summary_count;
- },
- getAllQuantities: function () {
- var res = 0;
- this.data.find('item quantite').each(function () {
- res += parseInt($(this).text());
- });
- return res;
},
updateIcon: function () {
},
openMenu: function (p1, p2, callback) {
- if (!this.dataok()) {
- callback();
- return;
- }
this.fluidbook.menu.quickCloseView();
return this.openCart(p2, callback);
},
openCart: function (p2, callback) {
- if (!this.checkLoginSession()) {
- callback();
- return;
- }
- if (!this.dataok()) {
- callback();
- return;
- }
- this._endMenu(this.fluidbook.l10n.__('your cart'), this.getCartContent(), function () {
- $('input[type="number"]').inputNumber();
-
+ this._endMenu('my cart', this.getCartContent(), function () {
callback();
});
},
- dataok: function () {
- return this.data !== undefined && this.data !== null;
- },
-
getCartContent: function () {
if (this.getItemsNumbers() == 0) {
return '<div class="cart-empty">' + this.fluidbook.l10n.__('your cart is empty') + '</div>';
}
- var $this = this;
- var content = '<table class="cart-items">';
- var totalht = 0;
- $.each(this.data.items, function (index, item) {
- var qty = $this.parseInt(item.qty);
- var unit = $this.parseFloat(item.product_price_value);
- var ht = unit * qty;
-
- var options = {};
- $.each(item.options, function (k, opt) {
- options[opt.option_id] = opt.option_value;
- });
+ var columns = ['Catalogue', 'Catégorie', 'Ligne', 'Sexe', 'Modèle', 'Couleur', 'Désignation', 'Tarif', 'PVC', 'Image', 'PACK'];
+ var $this = this;
+ var content = '<table class="cart-items" cellpadding="0" cellspacing="0">';
+ content += '<thead><tr>';
+ $.each(columns, function (k, v) {
+ content += '<th>' + v + '</th>';
+ });
+ content += '<th></th>';
+ content += '</tr></thead>';
+ content += '<tbody>';
+ $.each(this.items, function (i, ref) {
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>';
- var step = $this.minQuantities[item.product_sku];
- if (step === undefined) {
- step = 1;
- }
- content += '<td class="quantity"><input data-options=\'' + JSON.stringify(options) + '\' 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>';
+ $.each(columns, function (k, v) {
+ var value = $this.data[ref][v];
+ if (v === 'PVC' || v === 'Tarif') {
+ value = $this.formatPrice(value, '€');
+ } else if (v === 'Image') {
+ if ($this.data[ref] && $this.data[ref]['zoom_image']) {
+ value = '<img src="' + $this.data[ref]['zoom_image'] + '" />';
+ } else {
+ value = '-';
+ }
+ }
+ content += '<td>' + value + '</td>';
+ });
+ content += '<td><a href="#" data-cart-delete="' + i + '">' + getSpriteIcon('interface-close') + '</a></td>';
content += '</tr>';
-
- totalht += ht;
});
-
- content += '</table>';
- content += '<table class="cart-totals">';
- content += '<tr><td colspan="2" class="hr"></td></tr>';
- content += '<tr class="total"><td>' + $this.fluidbook.l10n.__('total') + '</td><td>' + $this.formatPrice($this.parseFloat(this.data.subtotalAmount)) + '</td></tr>';
+ content += '</tbody>';
content += '</table>';
-
content += '<div class="cart-footer">';
- content += '<div class="fonctions"><a href="#/closeview" class="back">' + $this.fluidbook.l10n.__('continue shopping') + '</a><a href="' + this.baseURL + 'checkout/cart" target="_blank">' + $this.fluidbook.l10n.__('proceed to checkout') + '</a></div>';
+ content += '<div class="fonctions"><a href="#" class="exportCartPDF">Export my cart (PDF)</a><a href="" class="exportCartXLS">Export my cart (XLS)</a></div>';
content += '</div>';
return content;
},
- getFormKey: function () {
- return $.cookie('form_key');
- },
-
- updateCartFromQuantities: function (el) {
- var $this = this;
- var input = $(el).find('input.cartqty');
- var item_id = $(input).attr('name');
- var options = $(input).data('options');
- var newVal = $this.parseInt($(input).val());
-
- if (newVal === 0) {
- this.removeFromCart(item_id, function () {
- });
- } else {
- this.fluidbook.displayLoader();
-
- $.ajax({
- url: $this.baseURL + 'checkout/cart/updateItemOptions/id/' + item_id,
- method: "post",
- data: {item: item_id, qty: newVal, super_attribute: options, form_key: this.getFormKey()},
- xhrFields: {withCredentials: true},
- success: function () {
- $this.updateCartData(function () {
-
- });
- }
- })
- }
- },
-
- updateCart: function () {
- if (!this.dataok()) {
- return;
- }
- $('[data-menu="cart"] .content').html(this.getCartContent());
- $('input[type="number"]').inputNumber();
- this.updateIcon();
- resize();
-
- },
-
formatPrice: function (price, suffix) {
if (typeof price == 'number') {
price = parseFloat(price);
suffix = '';
}
- return price.toLocaleString("fr-FR", {style: "currency", currency: "EUR"}) + ' ' + suffix;
+ return price.toLocaleString("fr-FR", {
+ style: "currency",
+ currency: "EUR",
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2
+ }) + ' ' + suffix;
},
_endMenu: function (title, content, callback) {
- var view = this.fluidbook.menu.getCaption(title);
+ var view = '<div id="pumacart">';
+ view += this.fluidbook.menu.getCaption(title);
view += '<div class="content">';
view += "" + content;
view += '</div>';
+ view += '</div>';
this.fluidbook.menu.viewWrap(view, 'cart');
callback();
},
+ getMenuWidth: function () {
+ return 1200;
+ },
+
parseFloat: function (s) {
if (typeof s === 'number') {
return s;