2015-03-03 09:25:30 +00:00
|
|
|
// 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
|
|
|
|
frappe.provide("shopping_cart");
|
|
|
|
|
2016-01-06 12:51:18 +00:00
|
|
|
frappe.ready(function() {
|
2014-10-21 10:46:30 +00:00
|
|
|
// update user
|
|
|
|
if(full_name) {
|
|
|
|
$('.navbar li[data-label="User"] a')
|
|
|
|
.html('<i class="icon-fixed-width icon-user"></i> ' + full_name);
|
|
|
|
}
|
|
|
|
// update login
|
|
|
|
shopping_cart.set_cart_count();
|
|
|
|
});
|
|
|
|
|
|
|
|
$.extend(shopping_cart, {
|
|
|
|
update_cart: function(opts) {
|
2015-09-28 13:34:42 +00:00
|
|
|
if(!full_name || full_name==="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,
|
2015-09-17 10:58:30 +00:00
|
|
|
with_items: opts.with_items || 0
|
2014-10-21 10:46:30 +00:00
|
|
|
},
|
|
|
|
btn: opts.btn,
|
|
|
|
callback: function(r) {
|
2016-01-06 11:11:50 +00:00
|
|
|
shopping_cart.set_cart_count();
|
2014-10-21 10:46:30 +00:00
|
|
|
if(opts.callback)
|
|
|
|
opts.callback(r);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set_cart_count: function() {
|
|
|
|
var cart_count = getCookie("cart_count");
|
2016-01-08 11:50:58 +00:00
|
|
|
|
|
|
|
if($(".cart-icon").length == 0) {
|
2016-04-15 09:22:23 +00:00
|
|
|
$('<div class="cart-icon small" style="float:right;padding:3px;border-radius:10px;\
|
2016-04-20 10:50:49 +00:00
|
|
|
border: 1px solid #7575ff;">\
|
|
|
|
<a href="/cart" style="color:#7575ff; text-decoration: none">\
|
2016-04-15 09:22:23 +00:00
|
|
|
Cart\
|
2016-04-20 10:50:49 +00:00
|
|
|
<span style="color:#7575ff;" class="badge" id="cart-count">5</span>\
|
2016-04-15 09:22:23 +00:00
|
|
|
</a></div>').appendTo($('.shopping-cart'))
|
2016-01-08 11:50:58 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 09:51:21 +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) {
|
2016-01-06 09:51:21 +00:00
|
|
|
$cart.css("display", "none");
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|