brotherton-erpnext/erpnext/public/js/shopping_cart.js

141 lines
3.3 KiB
JavaScript
Raw Normal View History

// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2014-10-21 10:46:30 +00:00
// License: GNU General Public License v3. See license.txt
// shopping cart
2017-05-30 07:24:42 +00:00
frappe.provide("erpnext.shopping_cart");
var shopping_cart = erpnext.shopping_cart;
2014-10-21 10:46:30 +00:00
frappe.ready(function() {
var full_name = frappe.session && frappe.session.user_fullname;
2014-10-21 10:46:30 +00:00
// update user
if(full_name) {
$('.navbar li[data-label="User"] a')
.html('<i class="fa fa-fixed-width fa fa-user"></i> ' + full_name);
2014-10-21 10:46:30 +00:00
}
2016-10-04 06:10:22 +00:00
2014-10-21 10:46:30 +00:00
// update login
2016-06-22 10:21:42 +00:00
shopping_cart.show_shoppingcart_dropdown();
2014-10-21 10:46:30 +00:00
shopping_cart.set_cart_count();
2016-06-22 10:21:42 +00:00
shopping_cart.bind_dropdown_cart_buttons();
2014-10-21 10:46:30 +00:00
});
$.extend(shopping_cart, {
2016-06-22 10:21:42 +00:00
show_shoppingcart_dropdown: function() {
$(".shopping-cart").on('shown.bs.dropdown', function() {
if (!$('.shopping-cart-menu .cart-container').length) {
return frappe.call({
method: 'erpnext.shopping_cart.cart.get_shopping_cart_menu',
callback: function(r) {
if (r.message) {
$('.shopping-cart-menu').html(r.message);
}
}
});
}
});
},
2016-10-04 06:10:22 +00:00
update_cart: function(opts) {
if(frappe.session.user==="Guest") {
2014-10-21 10:46:30 +00:00
if(localStorage) {
localStorage.setItem("last_visited", window.location.pathname);
}
window.location.href = "/login";
} else {
return frappe.call({
type: "POST",
method: "erpnext.shopping_cart.cart.update_cart",
args: {
item_code: opts.item_code,
qty: opts.qty,
with_items: opts.with_items || 0
2014-10-21 10:46:30 +00:00
},
btn: opts.btn,
callback: function(r) {
2016-10-04 06:10:22 +00:00
shopping_cart.set_cart_count();
2016-05-02 06:13:44 +00:00
if (r.message.shopping_cart_menu) {
$('.shopping-cart-menu').html(r.message.shopping_cart_menu);
2016-10-04 06:10:22 +00:00
}
2014-10-21 10:46:30 +00:00
if(opts.callback)
opts.callback(r);
}
});
}
},
set_cart_count: function() {
2018-03-09 10:58:25 +00:00
var cart_count = frappe.get_cookie("cart_count");
if(frappe.session.user==="Guest") {
cart_count = 0;
}
2016-10-04 06:10:22 +00:00
2016-05-02 06:13:44 +00:00
if(cart_count) {
$(".shopping-cart").toggleClass('hidden', false);
2016-10-04 06:10:22 +00:00
}
var $cart = $('.cart-icon');
var $badge = $cart.find("#cart-count");
2016-01-08 11:50:58 +00:00
if(parseInt(cart_count) === 0 || cart_count === undefined) {
$cart.css("display", "none");
$(".cart-items").html('Cart is Empty');
$(".cart-tax-items").hide();
$(".btn-place-order").hide();
$(".cart-addresses").hide();
}
else {
$cart.css("display", "inline");
}
2016-01-08 11:50:58 +00:00
2014-10-21 10:46:30 +00:00
if(cart_count) {
$badge.html(cart_count);
} else {
$badge.remove();
}
2016-06-22 10:21:42 +00:00
},
2016-10-04 06:10:22 +00:00
2016-06-22 10:21:42 +00:00
shopping_cart_update: function(item_code, newVal, cart_dropdown) {
frappe.freeze();
shopping_cart.update_cart({
item_code: item_code,
qty: newVal,
with_items: 1,
btn: this,
callback: function(r) {
frappe.unfreeze();
if(!r.exc) {
$(".cart-items").html(r.message.items);
$(".cart-tax-items").html(r.message.taxes);
if (cart_dropdown != true) {
$(".cart-icon").hide();
2016-10-04 06:10:22 +00:00
}
2016-06-22 10:21:42 +00:00
}
},
});
},
2016-10-04 06:10:22 +00:00
2017-05-30 07:24:42 +00:00
bind_dropdown_cart_buttons: function () {
2016-06-22 10:21:42 +00:00
$(".cart-icon").on('click', '.number-spinner button', function () {
var btn = $(this),
input = btn.closest('.number-spinner').find('input'),
oldValue = input.val().trim(),
newVal = 0;
2016-10-04 06:10:22 +00:00
2017-05-30 07:24:42 +00:00
if (btn.attr('data-dir') == 'up') {
newVal = parseInt(oldValue) + 1;
} else {
if (oldValue > 1) {
newVal = parseInt(oldValue) - 1;
}
2016-06-22 10:21:42 +00:00
}
input.val(newVal);
2016-10-04 06:10:22 +00:00
var item_code = input.attr("data-item-code");
2016-06-22 10:21:42 +00:00
shopping_cart.shopping_cart_update(item_code, newVal, true);
return false;
});
2016-10-04 06:10:22 +00:00
2016-06-22 10:21:42 +00:00
},
2016-10-04 06:10:22 +00:00
2014-10-21 10:46:30 +00:00
});