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,select");
12 $snipping.toggle(!!value);
13 $inputs.attr("readonly", value <= 0 ? null : "readonly" ).prop("readonly", value <= 0 ? null : "readonly" );
15 $inputs.each(function () {
16 $(this).val( data[$(this).attr("name")] || "" );
21 $(oe_website_sale).on('mouseup touchend', '.js_publish', function (ev) {
22 $(ev.currentTarget).parents(".thumbnail").toggleClass("disabled");
25 $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () {
27 var value = parseInt($input.val(), 10);
28 var line_id = parseInt($input.data('line-id'),10);
29 if (isNaN(value)) value = 0;
30 openerp.jsonRpc("/shop/cart/update_json", 'call', {
32 'product_id': parseInt($input.data('product-id'),10),
34 .then(function (data) {
39 var $q = $(".my_cart_quantity");
40 $q.parent().parent().removeClass("hidden", !data.quantity);
41 $q.html(data.cart_quantity).hide().fadeIn(600);
43 $input.val(data.quantity);
44 $('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
45 $("#cart_total").replaceWith(data['website_sale.total']);
49 // hack to add and rome from cart with json
50 $(oe_website_sale).on('click', 'a.js_add_cart_json', function (ev) {
52 var $link = $(ev.currentTarget);
53 var $input = $link.parent().parent().find("input");
54 var min = parseFloat($input.data("min") || 0);
55 var max = parseFloat($input.data("max") || Infinity);
56 var quantity = ($link.has(".fa-minus").length ? -1 : 1) + parseFloat($input.val(),10);
57 $input.val(quantity > min ? (quantity < max ? quantity : max) : min);
58 $('input[name="'+$input.attr("name")+'"]').val(quantity > min ? (quantity < max ? quantity : max) : min);
63 $('.a-submit', oe_website_sale).off('click').on('click', function () {
64 $(this).closest('form').submit();
66 $('form.js_attributes input, form.js_attributes select', oe_website_sale).on('change', function () {
67 $(this).closest("form").submit();
70 // change price when they are variants
71 $('form.js_add_cart_json label', oe_website_sale).on('mouseup touchend', function (ev) {
73 var $price = $label.parents("form:first").find(".oe_price .oe_currency_value");
74 if (!$price.data("price")) {
75 $price.data("price", parseFloat($price.text()));
77 var value = $price.data("price") + parseFloat($label.find(".badge span").text() || 0);
79 $price.html(value + (dec < 0.01 ? ".00" : (dec < 1 ? "0" : "") ));
81 // hightlight selected color
82 $('.css_attribute_color input', oe_website_sale).on('change', function (ev) {
83 $('.css_attribute_color').removeClass("active");
84 $('.css_attribute_color:has(input:checked)').addClass("active");
87 function price_to_str(price) {
88 price = Math.round(price * 100) / 100;
89 var dec = Math.round((price % 1) * 100);
90 return price + (dec ? '' : '.0') + (dec%10 ? '' : '0');
93 $(oe_website_sale).on('change', 'input.js_product_change', function (ev) {
94 var $parent = $(this).closest('.js_product');
95 $parent.find(".oe_default_price:first .oe_currency_value").html( price_to_str(+$(this).data('lst_price')) );
96 $parent.find(".oe_price:first .oe_currency_value").html(price_to_str(+$(this).data('price')) );
98 var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img, img.product_detail_img');
99 $img.attr("src", "/website/image/product.product/" + $(this).val() + "/image");
102 $(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change', function (ev) {
103 var $ul = $(this).parents('ul.js_add_cart_variants:first');
104 var $parent = $ul.closest('.js_product');
105 var $product_id = $parent.find('input.product_id').first();
106 var $price = $parent.find(".oe_price:first .oe_currency_value");
107 var $default_price = $parent.find(".oe_default_price:first .oe_currency_value");
108 var variant_ids = $ul.data("attribute_value_ids");
110 $parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () {
111 values.push(+$(this).val());
114 $parent.find("label").removeClass("text-muted css_not_available");
116 var product_id = false;
117 for (var k in variant_ids) {
118 if (_.isEmpty(_.difference(variant_ids[k][1], values))) {
119 $price.html(price_to_str(variant_ids[k][2]));
120 $default_price.html(price_to_str(variant_ids[k][3]));
121 if (variant_ids[k][3]-variant_ids[k][2]>0.2) {
122 $default_price.closest('.oe_website_sale').addClass("discount");
124 $default_price.closest('.oe_website_sale').removeClass("discount");
126 product_id = variant_ids[k][0];
132 var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img, img.product_detail_img');
133 $img.attr("src", "/website/image/product.product/" + product_id + "/image");
134 $img.parent().attr('data-oe-model', 'product.product').attr('data-oe-id', product_id)
135 .data('oe-model', 'product.product').data('oe-id', product_id);
138 $parent.find("input.js_variant_change:radio, select.js_variant_change").each(function () {
139 var $input = $(this);
140 var id = +$input.val();
143 $parent.find("ul:not(:has(input.js_variant_change[value='" + id + "'])) input.js_variant_change:checked, select").each(function () {
144 values.push(+$(this).val());
147 for (var k in variant_ids) {
148 if (!_.difference(values, variant_ids[k][1]).length) {
152 $input.closest("label").addClass("css_not_available");
153 $input.find("option[value='" + id + "']").addClass("css_not_available");
157 $parent.removeClass("css_not_available");
158 $product_id.val(product_id);
159 $parent.find(".js_check_product").removeAttr("disabled");
161 $parent.addClass("css_not_available");
163 $parent.find(".js_check_product").attr("disabled", "disabled");
166 $('ul.js_add_cart_variants', oe_website_sale).each(function () {
167 $('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
170 $(oe_website_sale).on('change', "select[name='country_id']", function () {
171 var $select = $("select[name='state_id']");
172 $select.find("option:not(:first)").hide();
173 var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
174 $select.parent().toggle(nb>1);
176 $(oe_website_sale).find("select[name='country_id']").change();
178 $(oe_website_sale).on('change', "select[name='shipping_country_id']", function () {
179 var $select = $("select[name='shipping_state_id']");
180 $select.find("option:not(:first)").hide();
181 var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
182 $select.parent().toggle(nb>1);
184 $(oe_website_sale).find("select[name='shipping_country_id']").change();