From 2dfe9f2bed60fb5df2f743421e07d72efad4c431 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Tue, 3 Sep 2013 14:34:38 +0200 Subject: [PATCH] [IMP] website_sale: add javascript for add/remove to basket for usability bzr revid: chm@openerp.com-20130903123438-h0lg0ufouovc09f3 --- addons/website_sale/controllers/main.py | 19 +++++--- .../static/src/js/website_sale.js | 44 +++++++++++++++---- addons/website_sale/views/website_sale.xml | 5 +-- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index 9d89829239d..1ee5f334536 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -6,6 +6,7 @@ from openerp.addons.web import http from openerp.addons.web.http import request import random import werkzeug +import simplejson def get_order(order_id=None): order_obj = request.registry.get('sale.order') @@ -203,7 +204,7 @@ class Ecommerce(http.Controller): order_line_id = order_line_obj.create(request.cr, SUPERUSER_ID, values, context=context) order.write({'order_line': [(4, order_line_id)]}, context=context) - return quantity + return [quantity, order.get_total_quantity()] @http.route(['/shop/mycart/'], type='http', auth="public") def mycart(self, **post): @@ -232,20 +233,24 @@ class Ecommerce(http.Controller): return website.render("website_sale.mycart", values) @http.route(['/shop//add_cart/', '/shop/add_cart/'], type='http', auth="public") - def add_cart(self, path=None, product_id=None, order_line_id=None, remove=None): - self.add_product_to_cart(product_id=product_id, order_line_id=order_line_id, number=(remove and -1 or 1)) + def add_cart(self, path=None, product_id=None, order_line_id=None, remove=None, json=None): + quantity = self.add_product_to_cart(product_id=product_id, order_line_id=order_line_id, number=(remove and -1 or 1)) + if json: + return simplejson.dumps(quantity) if path: return werkzeug.utils.redirect("/shop/%s/" % path) else: return werkzeug.utils.redirect("/shop/") @http.route(['/shop/remove_cart/', '/shop//remove_cart/'], type='http', auth="public") - def remove_cart(self, path=None, product_id=None, order_line_id=None): - return self.add_cart(product_id=product_id, order_line_id=order_line_id, path=path, remove=True) + def remove_cart(self, path=None, product_id=None, order_line_id=None, json=None): + return self.add_cart(product_id=product_id, order_line_id=order_line_id, path=path, remove=True, json=json) @http.route(['/shop/set_cart/', '/shop//set_cart/'], type='http', auth="public") - def set_cart(self, path=None, product_id=None, order_line_id=None, set_number=0): - self.add_product_to_cart(product_id=product_id, order_line_id=order_line_id, set_number=set_number) + def set_cart(self, path=None, product_id=None, order_line_id=None, set_number=0, json=None): + quantity = self.add_product_to_cart(product_id=product_id, order_line_id=order_line_id, set_number=set_number) + if json: + return simplejson.dumps(quantity) if path: return werkzeug.utils.redirect("/shop/%s/" % path) else: diff --git a/addons/website_sale/static/src/js/website_sale.js b/addons/website_sale/static/src/js/website_sale.js index 0a556a775b8..a10f6e8e012 100644 --- a/addons/website_sale/static/src/js/website_sale.js +++ b/addons/website_sale/static/src/js/website_sale.js @@ -3,14 +3,6 @@ $(document).ready(function () { $(".oe_website_sale .js_shipping").toggle(); }); - $(".oe_website_sale .oe_mycart input.js_quantity").change(function () { - var value = parseInt($(this).val()); - if (!isNaN(value)) { - window.location.href = window.location.origin + window.location.pathname + - 'set_cart/?order_line_id=' + $(this).data('id') + '&set_number=' + value; - } - }); - $payment = $(".oe_website_sale .js_payment"); $("input[name='payment_type']", $payment).click(function (ev) { var payment_id = $(ev.currentTarget).val(); @@ -23,4 +15,40 @@ $(document).ready(function () { $(ev.currentTarget).parents(".thumbnail").toggleClass("disabled"); }); + function set_my_cart_quantity(qty) { + $("#my_cart_quantity").html(qty.toString().indexOf(".") > -1 ? qty : qty + '.0'); + } + + $(".oe_website_sale .oe_mycart input.js_quantity").change(function () { + var $input = $(this); + var value = parseInt($input.val()); + if (!isNaN(value)) { + $.get("./set_cart/", {'order_line_id': $(this).data('id'), 'set_number': value, 'json': true}, function (data) { + var data = JSON.parse(data); + set_my_cart_quantity(data[1]); + $input.val(data[0]); + }); + } + }); + + // hack to add and rome from cart with json + $('.oe_website_sale a[href*="/add_cart/"], a[href*="/remove_cart/"]').on('click', function (ev) { + ev.preventDefault(); + $link = $(ev.currentTarget); + $.get($link.attr("href"), {'json': true}, function (data) { + var data = JSON.parse(data); + set_my_cart_quantity(data[1]); + $link.parent().find(".js_quantity").val(data[0]); + }); + return false; + }); + $('.oe_website_sale form[action*="/add_cart/"]').on('submit', function (ev) { + ev.preventDefault(); + $form = $(ev.currentTarget); + $.get($form.attr("action"), {'product_id': $form.find('input[name="product_id"]:checked').val(), 'json': true}, function (data) { + set_my_cart_quantity(JSON.parse(data)[1]); + }); + return false; + }); + }); diff --git a/addons/website_sale/views/website_sale.xml b/addons/website_sale/views/website_sale.xml index 0c54595fbed..bcfec8a0530 100644 --- a/addons/website_sale/views/website_sale.xml +++ b/addons/website_sale/views/website_sale.xml @@ -21,10 +21,7 @@
  • - My cart - - - + My cart