2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-04-18 10:45:31 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
frappe.ready(function() {
|
2015-10-05 10:57:52 +00:00
|
|
|
window.item_code = $('[itemscope] [itemprop="productID"]').text().trim();
|
2014-04-18 10:45:31 +00:00
|
|
|
var qty = 0;
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
frappe.call({
|
|
|
|
type: "POST",
|
2014-10-21 10:46:30 +00:00
|
|
|
method: "erpnext.shopping_cart.product.get_product_info",
|
2014-04-18 10:45:31 +00:00
|
|
|
args: {
|
|
|
|
item_code: "{{ name }}"
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
2015-09-11 13:19:59 +00:00
|
|
|
$(".item-cart").toggleClass("hide", !!!r.message.price);
|
2014-04-18 10:45:31 +00:00
|
|
|
if(r.message && r.message.price) {
|
|
|
|
$(".item-price")
|
|
|
|
.html(r.message.price.formatted_price + " per " + r.message.uom);
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
if(r.message.stock==0) {
|
|
|
|
$(".item-stock").html("<div class='help'>Not in stock</div>");
|
|
|
|
}
|
|
|
|
else if(r.message.stock==1) {
|
|
|
|
$(".item-stock").html("<div style='color: green'>\
|
|
|
|
<i class='icon-check'></i> Available (in stock)</div>");
|
|
|
|
}
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
if(r.message.qty) {
|
|
|
|
qty = r.message.qty;
|
2015-09-11 13:19:59 +00:00
|
|
|
toggle_update_cart(r.message.qty);
|
|
|
|
} else {
|
|
|
|
toggle_update_cart(0);
|
2014-04-18 10:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
$("#item-add-to-cart button").on("click", function() {
|
|
|
|
shopping_cart.update_cart({
|
2015-10-05 10:57:52 +00:00
|
|
|
item_code: get_item_code(),
|
2014-04-18 10:45:31 +00:00
|
|
|
qty: 1,
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
toggle_update_cart(1);
|
|
|
|
qty = 1;
|
|
|
|
}
|
|
|
|
},
|
2015-09-11 13:19:59 +00:00
|
|
|
btn: this,
|
2014-04-18 10:45:31 +00:00
|
|
|
});
|
|
|
|
});
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
$("#item-update-cart button").on("click", function() {
|
|
|
|
shopping_cart.update_cart({
|
2015-10-05 10:57:52 +00:00
|
|
|
item_code: get_item_code(),
|
2014-04-18 10:45:31 +00:00
|
|
|
qty: $("#item-update-cart input").val(),
|
|
|
|
btn: this,
|
|
|
|
callback: function(r) {
|
|
|
|
if(r.exc) {
|
|
|
|
$("#item-update-cart input").val(qty);
|
|
|
|
} else {
|
|
|
|
qty = $("#item-update-cart input").val();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var toggle_update_cart = function(qty) {
|
|
|
|
$("#item-add-to-cart").toggle(qty ? false : true);
|
|
|
|
$("#item-update-cart")
|
|
|
|
.toggle(qty ? true : false)
|
|
|
|
.find("input").val(qty);
|
2015-09-11 13:19:59 +00:00
|
|
|
}
|
2015-10-05 10:57:52 +00:00
|
|
|
|
|
|
|
function get_item_code() {
|
|
|
|
if(window.variant_info) {
|
|
|
|
attributes = {};
|
|
|
|
$('[itemscope]').find(".item-view-attribute select").each(function() {
|
|
|
|
attributes[$(this).attr('data-attribute')] = $(this).val();
|
|
|
|
});
|
|
|
|
for(var i in variant_info) {
|
|
|
|
var variant = variant_info[i];
|
|
|
|
var match = true;
|
|
|
|
for(var j in variant.attributes) {
|
|
|
|
if(attributes[variant.attributes[j].attribute]
|
|
|
|
!= variant.attributes[j].attribute_value) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(match) {
|
|
|
|
return variant.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw "Unable to match variant";
|
|
|
|
} else {
|
|
|
|
return item_code;
|
|
|
|
}
|
|
|
|
}
|