// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
// js inside blog page
// shopping cart
frappe.provide("shopping_cart");
$.extend(shopping_cart, {
show_error: function(title, text) {
$("#cart-container").html('
' +
title + '
' + text + '
');
},
bind_events: function() {
shopping_cart.bind_address_select();
shopping_cart.bind_place_order();
shopping_cart.bind_change_qty();
},
bind_address_select: function() {
$(".cart-addresses").find('input[data-address-name]').on("click", function() {
if($(this).prop("checked")) {
var me = this;
return frappe.call({
type: "POST",
method: "erpnext.shopping_cart.cart.update_cart_address",
args: {
address_fieldname: $(this).attr("data-fieldname"),
address_name: $(this).attr("data-address-name")
},
callback: function(r) {
if(!r.exc) {
$(".cart-tax-items").html(r.message.taxes);
}
}
});
} else {
return false;
}
});
},
bind_place_order: function() {
$(".btn-place-order").on("click", function() {
shopping_cart.place_order(this);
});
},
bind_change_qty: function() {
// bind update button
$(".cart-items").on("change", ".cart-qty", function() {
var item_code = $(this).attr("data-item-code");
frappe.freeze();
shopping_cart.update_cart({
item_code: item_code,
qty: $(this).val(),
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);
$(".cart-icon").hide();
}
},
});
});
},
render_tax_row: function($cart_taxes, doc, shipping_rules) {
var shipping_selector;
if(shipping_rules) {
shipping_selector = '';
}
var $tax_row = $(repl('\
\
\
' +
(shipping_selector || '
%(description)s
') +
'
\
\
\
\
%(formatted_tax_amount)s
\
\
', doc)).appendTo($cart_taxes);
if(shipping_selector) {
$tax_row.find('select option').each(function(i, opt) {
if($(opt).html() == doc.description) {
$(opt).attr("selected", "selected");
}
});
$tax_row.find('select').on("change", function() {
shopping_cart.apply_shipping_rule($(this).val(), this);
});
}
},
apply_shipping_rule: function(rule, btn) {
return frappe.call({
btn: btn,
type: "POST",
method: "erpnext.shopping_cart.cart.apply_shipping_rule",
args: { shipping_rule: rule },
callback: function(r) {
if(!r.exc) {
shopping_cart.render(r.message);
}
}
});
},
place_order: function(btn) {
return frappe.call({
type: "POST",
method: "erpnext.shopping_cart.cart.place_order",
btn: btn,
callback: function(r) {
if(r.exc) {
var msg = "";
if(r._server_messages) {
msg = JSON.parse(r._server_messages || []).join("
");
}
$("#cart-error")
.empty()
.html(msg || frappe._("Something went wrong!"))
.toggle(true);
} else {
window.location.href = "/orders/" + encodeURIComponent(r.message);
}
}
});
}
});
$(document).ready(function() {
$(".cart-icon").hide();
shopping_cart.bind_events();
});
function show_terms() {
var html = $(".cart-terms").html();
frappe.msgprint(html);
}