1 $(document).ready(function () {
2 $('.oe_website_sale').each(function () {
3 var oe_website_sale = this;
5 var $shippingDifferent = $("select[name='shipping_id']", oe_website_sale);
6 $shippingDifferent.change(function (event) {
7 var value = +$shippingDifferent.val();
8 var data = $shippingDifferent.find("option:selected").data();
9 var $snipping = $(".js_shipping", oe_website_sale);
10 var $inputs = $snipping.find("input");
11 var $selects = $snipping.find("select");
13 $snipping.toggle(!!value);
14 $inputs.attr("readonly", value <= 0 ? null : "readonly" ).prop("readonly", value <= 0 ? null : "readonly" );
15 $selects.attr("disabled", value <= 0 ? null : "disabled" ).prop("disabled", value <= 0 ? null : "disabled" );
17 $inputs.each(function () {
18 $(this).val( data[$(this).attr("name")] || "" );
23 $(oe_website_sale).on('mouseup touchend', '.js_publish', function (ev) {
24 $(ev.currentTarget).parents(".thumbnail").toggleClass("disabled");
27 $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () {
29 var value = parseInt($input.val(), 10);
30 var line_id = parseInt($input.data('line-id'),10);
31 if (isNaN(value)) value = 0;
32 openerp.jsonRpc("/shop/cart/update_json", 'call', {
34 'product_id': parseInt($input.data('product-id'),10),
36 .then(function (data) {
41 var $q = $(".my_cart_quantity");
42 $q.parent().parent().removeClass("hidden", !data.quantity);
43 $q.html(data.cart_quantity).hide().fadeIn(600);
45 $input.val(data.quantity);
46 $('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
47 $("#cart_total").replaceWith(data['website_sale.total']);
51 // hack to add and rome from cart with json
52 $(oe_website_sale).on('click', 'a.js_add_cart_json', function (ev) {
54 var $link = $(ev.currentTarget);
55 var $input = $link.parent().parent().find("input");
56 var min = parseFloat($input.data("min") || 0);
57 var max = parseFloat($input.data("max") || Infinity);
58 var quantity = ($link.has(".fa-minus").length ? -1 : 1) + parseFloat($input.val(),10);
59 $input.val(quantity > min ? (quantity < max ? quantity : max) : min);
60 $('input[name="'+$input.attr("name")+'"]').val(quantity > min ? (quantity < max ? quantity : max) : min);
65 $('.oe_website_sale .a-submit, #comment .a-submit').off('click').on('click', function () {
66 $(this).closest('form').submit();
68 $('form.js_attributes input, form.js_attributes select', oe_website_sale).on('change', function () {
69 $(this).closest("form").submit();
72 // change price when they are variants
73 $('form.js_add_cart_json label', oe_website_sale).on('mouseup touchend', function (ev) {
75 var $price = $label.parents("form:first").find(".oe_price .oe_currency_value");
76 if (!$price.data("price")) {
77 $price.data("price", parseFloat($price.text()));
79 var value = $price.data("price") + parseFloat($label.find(".badge span").text() || 0);
81 $price.html(value + (dec < 0.01 ? ".00" : (dec < 1 ? "0" : "") ));
83 // hightlight selected color
84 $('.css_attribute_color input', oe_website_sale).on('change', function (ev) {
85 $('.css_attribute_color').removeClass("active");
86 $('.css_attribute_color:has(input:checked)').addClass("active");
89 function price_to_str(price) {
90 price = Math.round(price * 100) / 100;
91 var dec = Math.round((price % 1) * 100);
92 return price + (dec ? '' : '.0') + (dec%10 ? '' : '0');
95 $(oe_website_sale).on('change', 'input.js_product_change', function (ev) {
96 var $parent = $(this).closest('.js_product');
97 $parent.find(".oe_default_price:first .oe_currency_value").html( price_to_str(+$(this).data('lst_price')) );
98 $parent.find(".oe_price:first .oe_currency_value").html(price_to_str(+$(this).data('price')) );
100 var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img:first, img.product_detail_img');
101 $img.attr("src", "/website/image/product.product/" + $(this).val() + "/image");
104 $(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change', function (ev) {
105 var $ul = $(this).parents('ul.js_add_cart_variants:first');
106 var $parent = $ul.closest('.js_product');
107 var $product_id = $parent.find('input.product_id').first();
108 var $price = $parent.find(".oe_price:first .oe_currency_value");
109 var $default_price = $parent.find(".oe_default_price:first .oe_currency_value");
110 var variant_ids = $ul.data("attribute_value_ids");
112 $parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () {
113 values.push(+$(this).val());
116 $parent.find("label").removeClass("text-muted css_not_available");
118 var product_id = false;
119 for (var k in variant_ids) {
120 if (_.isEmpty(_.difference(variant_ids[k][1], values))) {
121 $price.html(price_to_str(variant_ids[k][2]));
122 $default_price.html(price_to_str(variant_ids[k][3]));
123 if (variant_ids[k][3]-variant_ids[k][2]>0.2) {
124 $default_price.closest('.oe_website_sale').addClass("discount");
126 $default_price.closest('.oe_website_sale').removeClass("discount");
128 product_id = variant_ids[k][0];
134 var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img:first, img.product_detail_img');
135 $img.attr("src", "/website/image/product.product/" + product_id + "/image");
136 $img.parent().attr('data-oe-model', 'product.product').attr('data-oe-id', product_id)
137 .data('oe-model', 'product.product').data('oe-id', product_id);
140 $parent.find("input.js_variant_change:radio, select.js_variant_change").each(function () {
141 var $input = $(this);
142 var id = +$input.val();
145 $parent.find("ul:not(:has(input.js_variant_change[value='" + id + "'])) input.js_variant_change:checked, select").each(function () {
146 values.push(+$(this).val());
149 for (var k in variant_ids) {
150 if (!_.difference(values, variant_ids[k][1]).length) {
154 $input.closest("label").addClass("css_not_available");
155 $input.find("option[value='" + id + "']").addClass("css_not_available");
159 $parent.removeClass("css_not_available");
160 $product_id.val(product_id);
161 $parent.find(".js_check_product").removeAttr("disabled");
163 $parent.addClass("css_not_available");
165 $parent.find(".js_check_product").attr("disabled", "disabled");
168 $('ul.js_add_cart_variants', oe_website_sale).each(function () {
169 $('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
172 $(oe_website_sale).on('change', "select[name='country_id']", function () {
173 var $select = $("select[name='state_id']");
174 $select.find("option:not(:first)").hide();
175 var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
176 $select.parent().toggle(nb>1);
178 $(oe_website_sale).find("select[name='country_id']").change();
180 $(oe_website_sale).on('change', "select[name='shipping_country_id']", function () {
181 var $select = $("select[name='shipping_state_id']");
182 $select.find("option:not(:first)").hide();
183 var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
184 $select.parent().toggle(nb>1);
186 $(oe_website_sale).find("select[name='shipping_country_id']").change();