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
|
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
|
|
|
|
2019-08-12 08:09:25 +00:00
|
|
|
var getParams = function (url) {
|
|
|
|
var params = [];
|
|
|
|
var parser = document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
var query = parser.search.substring(1);
|
|
|
|
var vars = query.split('&');
|
|
|
|
for (var i = 0; i < vars.length; i++) {
|
|
|
|
var pair = vars[i].split('=');
|
|
|
|
params[pair[0]] = decodeURIComponent(pair[1]);
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
2016-01-06 12:51:18 +00:00
|
|
|
frappe.ready(function() {
|
2017-06-20 07:21:32 +00:00
|
|
|
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')
|
2016-12-05 08:47:26 +00:00
|
|
|
.html('<i class="fa fa-fixed-width fa fa-user"></i> ' + full_name);
|
2014-10-21 10:46:30 +00:00
|
|
|
}
|
2019-08-12 08:09:25 +00:00
|
|
|
// set coupon code and sales partner code
|
2019-10-15 09:12:09 +00:00
|
|
|
|
|
|
|
var url_args = getParams(window.location.href);
|
|
|
|
|
|
|
|
var referral_coupon_code = url_args['cc'];
|
|
|
|
var referral_sales_partner = url_args['sp'];
|
|
|
|
|
2019-08-12 08:09:25 +00:00
|
|
|
var d = new Date();
|
|
|
|
// expires within 30 minutes
|
|
|
|
d.setTime(d.getTime() + (0.02 * 24 * 60 * 60 * 1000));
|
|
|
|
var expires = "expires="+d.toUTCString();
|
|
|
|
if (referral_coupon_code) {
|
|
|
|
document.cookie = "referral_coupon_code=" + referral_coupon_code + ";" + expires + ";path=/";
|
|
|
|
}
|
|
|
|
if (referral_sales_partner) {
|
|
|
|
document.cookie = "referral_sales_partner=" + referral_sales_partner + ";" + expires + ";path=/";
|
|
|
|
}
|
|
|
|
referral_coupon_code=frappe.get_cookie("referral_coupon_code");
|
|
|
|
referral_sales_partner=frappe.get_cookie("referral_sales_partner");
|
2016-10-04 06:10:22 +00:00
|
|
|
|
2019-08-12 08:09:25 +00:00
|
|
|
if (referral_coupon_code && $(".tot_quotation_discount").val()==undefined ) {
|
|
|
|
$(".txtcoupon").val(referral_coupon_code);
|
|
|
|
}
|
|
|
|
if (referral_sales_partner) {
|
|
|
|
$(".txtreferral_sales_partner").val(referral_sales_partner);
|
|
|
|
}
|
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();
|
2020-07-23 10:28:47 +00:00
|
|
|
shopping_cart.show_cart_navbar();
|
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({
|
2021-02-25 08:26:38 +00:00
|
|
|
method: 'erpnext.e_commerce.shopping_cart.cart.get_shopping_cart_menu',
|
2016-06-22 10:21:42 +00:00
|
|
|
callback: function(r) {
|
|
|
|
if (r.message) {
|
|
|
|
$('.shopping-cart-menu').html(r.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-10-04 06:10:22 +00:00
|
|
|
|
2017-06-20 08:35:36 +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",
|
2021-02-25 08:26:38 +00:00
|
|
|
method: "erpnext.e_commerce.shopping_cart.cart.update_cart",
|
2014-10-21 10:46:30 +00:00
|
|
|
args: {
|
|
|
|
item_code: opts.item_code,
|
|
|
|
qty: opts.qty,
|
2019-03-19 06:18:32 +00:00
|
|
|
additional_notes: opts.additional_notes !== undefined ? opts.additional_notes : undefined,
|
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-10-04 06:10:22 +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() {
|
2018-02-20 08:51:50 +00:00
|
|
|
var cart_count = frappe.get_cookie("cart_count");
|
2017-11-17 06:11:35 +00:00
|
|
|
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) {
|
2016-11-14 07:43:53 +00:00
|
|
|
$(".shopping-cart").toggleClass('hidden', false);
|
2016-10-04 06:10:22 +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");
|
2016-05-10 11:49:47 +00:00
|
|
|
$(".cart-items").html('Cart is Empty');
|
|
|
|
$(".cart-tax-items").hide();
|
|
|
|
$(".btn-place-order").hide();
|
|
|
|
$(".cart-addresses").hide();
|
2016-01-06 09:51:21 +00:00
|
|
|
}
|
|
|
|
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);
|
2021-03-11 15:54:47 +00:00
|
|
|
$cart.addClass('cart-animate');
|
|
|
|
setTimeout(() => {
|
|
|
|
$cart.removeClass('cart-animate');
|
|
|
|
}, 500);
|
2014-10-21 10:46:30 +00:00
|
|
|
} else {
|
|
|
|
$badge.remove();
|
|
|
|
}
|
2016-06-22 10:21:42 +00:00
|
|
|
},
|
2016-10-04 06:10:22 +00:00
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
shopping_cart_update: function({item_code, qty, cart_dropdown, additional_notes}) {
|
2016-06-22 10:21:42 +00:00
|
|
|
frappe.freeze();
|
|
|
|
shopping_cart.update_cart({
|
2019-03-19 06:18:32 +00:00
|
|
|
item_code,
|
|
|
|
qty,
|
|
|
|
additional_notes,
|
2016-06-22 10:21:42 +00:00
|
|
|
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");
|
2019-03-19 06:18:32 +00:00
|
|
|
shopping_cart.shopping_cart_update({item_code, qty: newVal, cart_dropdown: true});
|
2016-06-22 10:21:42 +00:00
|
|
|
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
|
|
|
|
2020-07-23 10:28:47 +00:00
|
|
|
show_cart_navbar: function () {
|
|
|
|
frappe.call({
|
2021-02-16 13:15:36 +00:00
|
|
|
method: "erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings.is_cart_enabled",
|
2020-07-23 10:28:47 +00:00
|
|
|
callback: function(r) {
|
|
|
|
$(".shopping-cart").toggleClass('hidden', r.message ? false : true);
|
|
|
|
}
|
|
|
|
});
|
2021-03-15 18:35:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
animate_add_to_cart(button) {
|
|
|
|
// Create 'added to cart' animation
|
|
|
|
let btn_id = "#" + button[0].id;
|
|
|
|
this.toggle_button_class(button, 'not-added', 'added-to-cart');
|
|
|
|
$(btn_id).text('Added to Cart');
|
|
|
|
|
|
|
|
// undo
|
|
|
|
setTimeout(() => {
|
|
|
|
this.toggle_button_class(button, 'added-to-cart', 'not-added');
|
|
|
|
$(btn_id).text('Add to Cart');
|
|
|
|
}, 2000);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle_button_class(button, remove, add) {
|
|
|
|
button.removeClass(remove);
|
|
|
|
button.addClass(add);
|
|
|
|
},
|
|
|
|
|
|
|
|
bind_add_to_cart_action() {
|
|
|
|
$('.page_content').on('click', '.btn-add-to-cart-list', (e) => {
|
|
|
|
const $btn = $(e.currentTarget);
|
|
|
|
$btn.prop('disabled', true);
|
|
|
|
|
|
|
|
this.animate_add_to_cart($btn);
|
|
|
|
|
|
|
|
const item_code = $btn.data('item-code');
|
|
|
|
erpnext.shopping_cart.update_cart({
|
|
|
|
item_code,
|
|
|
|
qty: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2020-07-23 10:28:47 +00:00
|
|
|
}
|
2021-03-15 18:35:53 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
});
|