[IMP] website_sale: add javascript for add/remove to basket for usability

bzr revid: chm@openerp.com-20130903123438-h0lg0ufouovc09f3
This commit is contained in:
Christophe Matthieu 2013-09-03 14:34:38 +02:00
parent cabe9fe9f7
commit 2dfe9f2bed
3 changed files with 49 additions and 19 deletions

View File

@ -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/<path:path>/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/<path:path>/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/<path:path>/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:

View File

@ -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;
});
});

View File

@ -21,10 +21,7 @@
<li>
<a href="/shop/mycart/">
<i class="icon-shopping-cart"></i>
My cart
<t t-if="order and order.get_total_quantity()">
<span class="badge" t-esc="order.get_total_quantity()"/>
</t>
My cart <span id="my_cart_quantity" t-attf-class="badge #{(not order or not order.get_total_quantity()) and 'hidden' or ''}" t-esc="order.get_total_quantity()"/>
</a>
</li>
</xpath>