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

74 lines
1.8 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
frappe.provide("shopping_cart");
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) {
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,
with_items: opts.with_items || 0
2014-10-21 10:46:30 +00:00
},
btn: opts.btn,
callback: function(r) {
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) {
$('<div class="cart-icon" style="float:right;padding-top:10px;">\
<a href="/cart" class="text-muted small">\
<div class="btn btn-primary cart"> Cart\
<span id="cart-count" class="label" style="padding-left:5px;margin-left:5px;\
margin-right:-5px;background-color: #2905E2;">\
</span>\
</div>\
</a></div>').appendTo($('.hidden-xs'))
}
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");
}
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();
}
}
});