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: {
|
2015-10-20 12:00:02 +00:00
|
|
|
item_code: get_item_code()
|
2014-04-18 10:45:31 +00:00
|
|
|
},
|
|
|
|
callback: function(r) {
|
2017-04-13 13:15:09 +00:00
|
|
|
$(".item-cart").toggleClass("hide", (!!!r.message.price || !!!r.message.in_stock));
|
2014-04-18 10:45:31 +00:00
|
|
|
if(r.message && r.message.price) {
|
|
|
|
$(".item-price")
|
2017-06-20 07:21:32 +00:00
|
|
|
.html(r.message.price.formatted_price + " {{ _("per") }} " + r.message.uom);
|
2015-09-11 13:19:59 +00:00
|
|
|
|
2017-03-21 10:58:03 +00:00
|
|
|
if(r.message.in_stock==0) {
|
2017-06-20 07:21:32 +00:00
|
|
|
$(".item-stock").html("<div style='color: red'> <i class='fa fa-close'></i> {{ _("Not in stock") }}</div>");
|
2014-04-18 10:45:31 +00:00
|
|
|
}
|
2017-03-21 10:58:03 +00:00
|
|
|
else if(r.message.in_stock==1) {
|
2017-06-20 07:21:32 +00:00
|
|
|
var qty_display = "{{ _("In stock") }}";
|
2017-03-21 10:58:03 +00:00
|
|
|
if (r.message.show_stock_qty) {
|
2017-06-20 07:21:32 +00:00
|
|
|
qty_display += " ("+r.message.stock_qty+")";
|
2017-03-21 10:58:03 +00:00
|
|
|
}
|
2014-04-18 10:45:31 +00:00
|
|
|
$(".item-stock").html("<div style='color: green'>\
|
2017-06-20 07:21:32 +00:00
|
|
|
<i class='fa fa-check'></i> "+qty_display+"</div>");
|
2014-04-18 10:45:31 +00:00
|
|
|
}
|
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() {
|
2017-05-30 07:24:42 +00:00
|
|
|
frappe.provide('erpnext.shopping_cart');
|
|
|
|
|
|
|
|
erpnext.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
|
|
|
|
2015-10-26 14:27:31 +00:00
|
|
|
$("[itemscope] .item-view-attribute .form-control").on("change", function() {
|
|
|
|
try {
|
|
|
|
var item_code = encodeURIComponent(get_item_code());
|
2015-10-29 07:50:07 +00:00
|
|
|
|
2015-10-26 14:27:31 +00:00
|
|
|
} catch(e) {
|
|
|
|
// unable to find variant
|
|
|
|
// then chose the closest available one
|
|
|
|
|
|
|
|
var attribute = $(this).attr("data-attribute");
|
|
|
|
var attribute_value = $(this).val()
|
2015-10-29 07:50:07 +00:00
|
|
|
var item_code = find_closest_match(attribute, attribute_value);
|
2015-10-26 14:27:31 +00:00
|
|
|
|
|
|
|
if (!item_code) {
|
2017-05-30 07:24:42 +00:00
|
|
|
frappe.msgprint(__("Cannot find a matching Item. Please select some other value for {0}.", [attribute]))
|
2015-10-26 14:27:31 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 12:00:02 +00:00
|
|
|
if (window.location.search.indexOf(item_code)!==-1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-01 11:53:12 +00:00
|
|
|
window.location.href = window.location.pathname + "?variant=" + item_code;
|
2015-10-20 12:00:02 +00:00
|
|
|
});
|
2014-04-18 10:45:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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() {
|
2017-05-30 07:24:42 +00:00
|
|
|
var variant_info = window.variant_info;
|
|
|
|
if(variant_info) {
|
2015-10-26 14:27:31 +00:00
|
|
|
var attributes = get_selected_attributes();
|
2015-10-29 07:50:07 +00:00
|
|
|
var no_of_attributes = Object.keys(attributes).length;
|
2015-10-26 14:27:31 +00:00
|
|
|
|
2015-10-05 10:57:52 +00:00
|
|
|
for(var i in variant_info) {
|
|
|
|
var variant = variant_info[i];
|
2015-10-29 07:50:07 +00:00
|
|
|
|
|
|
|
if (variant.attributes.length < no_of_attributes) {
|
|
|
|
// the case when variant has less attributes than template
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-05 10:57:52 +00:00
|
|
|
var match = true;
|
|
|
|
for(var j in variant.attributes) {
|
|
|
|
if(attributes[variant.attributes[j].attribute]
|
2017-05-30 07:24:42 +00:00
|
|
|
!= variant.attributes[j].attribute_value
|
|
|
|
) {
|
|
|
|
match = false;
|
|
|
|
break;
|
2015-10-05 10:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(match) {
|
|
|
|
return variant.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw "Unable to match variant";
|
|
|
|
} else {
|
2017-05-30 07:24:42 +00:00
|
|
|
return window.item_code;
|
2015-10-05 10:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-26 14:27:31 +00:00
|
|
|
|
2015-10-29 07:50:07 +00:00
|
|
|
function find_closest_match(selected_attribute, selected_attribute_value) {
|
2015-10-26 14:27:31 +00:00
|
|
|
// find the closest match keeping the selected attribute in focus and get the item code
|
|
|
|
|
|
|
|
var attributes = get_selected_attributes();
|
|
|
|
|
|
|
|
var previous_match_score = 0;
|
2015-10-29 07:50:07 +00:00
|
|
|
var previous_no_of_attributes = 0;
|
2015-10-26 14:27:31 +00:00
|
|
|
var matched;
|
2015-10-29 07:50:07 +00:00
|
|
|
|
2017-05-30 07:24:42 +00:00
|
|
|
var variant_info = window.variant_info;
|
2015-10-26 14:27:31 +00:00
|
|
|
for(var i in variant_info) {
|
|
|
|
var variant = variant_info[i];
|
|
|
|
var match_score = 0;
|
|
|
|
var has_selected_attribute = false;
|
|
|
|
|
|
|
|
for(var j in variant.attributes) {
|
|
|
|
if(attributes[variant.attributes[j].attribute]===variant.attributes[j].attribute_value) {
|
|
|
|
match_score = match_score + 1;
|
|
|
|
|
|
|
|
if (variant.attributes[j].attribute==selected_attribute && variant.attributes[j].attribute_value==selected_attribute_value) {
|
|
|
|
has_selected_attribute = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:50:07 +00:00
|
|
|
if (has_selected_attribute
|
|
|
|
&& ((match_score > previous_match_score) || (match_score==previous_match_score && previous_no_of_attributes < variant.attributes.length))) {
|
2015-10-26 14:27:31 +00:00
|
|
|
previous_match_score = match_score;
|
|
|
|
matched = variant;
|
2015-10-29 07:50:07 +00:00
|
|
|
previous_no_of_attributes = variant.attributes.length;
|
|
|
|
|
|
|
|
|
2015-10-26 14:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matched) {
|
|
|
|
for (var j in matched.attributes) {
|
|
|
|
var attr = matched.attributes[j];
|
|
|
|
$('[itemscope]')
|
|
|
|
.find(repl('.item-view-attribute .form-control[data-attribute="%(attribute)s"]', attr))
|
|
|
|
.val(attr.attribute_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matched.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_selected_attributes() {
|
|
|
|
var attributes = {};
|
|
|
|
$('[itemscope]').find(".item-view-attribute .form-control").each(function() {
|
|
|
|
attributes[$(this).attr('data-attribute')] = $(this).val();
|
|
|
|
});
|
|
|
|
return attributes;
|
|
|
|
}
|