[IMP] website_sale: render all order-lines and change cart quantity

* update cart code simplification
* fix rendering bug when change pricelist
* remove get_unit_price and fix rendering error from website_event_sale
This commit is contained in:
Christophe Matthieu
2015-10-01 10:26:29 +02:00
parent 9927f960f1
commit 66871ee65b
6 changed files with 128 additions and 147 deletions
@@ -79,7 +79,7 @@
</xpath>
</template>
<template id="cart" inherit_id="website_sale.cart" name="Hide product reduction for event tickets">
<template id="cart" inherit_id="website_sale.cart_lines" name="Hide product reduction for event tickets">
<xpath expr="//del" position="attributes">
<attribute name="t-attf-class" separator=" " add="#{line.event_id and 'hidden' or ''}"/>
</xpath>
+8 -3
View File
@@ -363,7 +363,7 @@ class website_sale(http.Controller):
return request.redirect("/shop/cart")
@http.route(['/shop/cart/update_json'], type='json', auth="public", methods=['POST'], website=True)
def cart_update_json(self, product_id, line_id, add_qty=None, set_qty=None, display=True):
def cart_update_json(self, product_id, line_id=None, add_qty=None, set_qty=None, display=True):
order = request.website.sale_get_order(force_create=1)
if order.state != 'draft':
request.website.sale_reset()
@@ -375,9 +375,14 @@ class website_sale(http.Controller):
return {}
if not display:
return None
order = request.website.sale_get_order()
value['cart_quantity'] = order.cart_quantity
value['website_sale.total'] = request.website._render("website_sale.total", {
'website_sale_order': request.website.sale_get_order()
value['website_sale.cart_lines'] = request.website._render("website_sale.cart_lines", {
'website_sale_order': order,
'compute_currency': lambda price: request.registry['res.currency']._compute(request.cr, request.uid,
order.company_id.currency_id, order.pricelist_id.currency_id, price, context=request.context),
'suggested_products': order.with_context(pricelist=order.pricelist_id.id)._cart_accessories()
})
return value
@@ -98,14 +98,17 @@ $('.oe_website_sale').each(function () {
product_ids.push(entry[0]);});
var qty = $(event.target).closest('form').find('input[name="add_qty"]').val();
ajax.jsonRpc("/shop/get_unit_price", 'call', {'product_ids': product_ids,'add_qty': parseInt(qty)})
.then(function (data) {
var current = product_dom.data("attribute_value_ids");
for(var j=0; j < current.length; j++){
current[j][2] = data[current[j][0]];
}
product_dom.attr("data-attribute_value_ids", JSON.stringify(current)).trigger("change");
});
if ($("#product_detail").length) {
// display the reduction from the pricelist in function of the quantity
ajax.jsonRpc("/shop/get_unit_price", 'call', {'product_ids': product_ids,'add_qty': parseInt(qty)})
.then(function (data) {
var current = product_dom.data("attribute_value_ids");
for(var j=0; j < current.length; j++){
current[j][2] = data[current[j][0]];
}
product_dom.attr("data-attribute_value_ids", JSON.stringify(current)).trigger("change");
});
}
});
// change for css
@@ -121,7 +124,7 @@ $('.oe_website_sale').each(function () {
};
})();
$(oe_website_sale).find(".oe_cart input.js_quantity").on("change", function () {
$(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () {
var $input = $(this);
if ($input.data('update_change')) {
return;
@@ -140,65 +143,46 @@ $('.oe_website_sale').each(function () {
});
if (isNaN(value)) value = 0;
$input.data('update_change', true);
if ($(this).hasClass('js_no_gup')) { // if get_unit_price (gup) not needed
var gup = $.when();
}
else {
var gup = ajax.jsonRpc("/shop/get_unit_price", 'call', {
'product_ids': product_ids,
'add_qty': value,
'use_order_pricelist': true})
.then(function (res) {
//basic case
$dom.find('span.oe_currency_value').last().text(res[product_id].toFixed(2));
$dom.find('.text-danger').toggle(res[product_id]<default_price && (default_price-res[product_id] > default_price/100));
//optional case
$dom_optional.each(function(){
var id = $(this).find('span[data-product-id]').data('product-id');
var price = parseFloat($(this).find(".text-danger > span.oe_currency_value").text());
$(this).find("span.oe_currency_value").last().text(res[id].toFixed(2));
$(this).find('.text-danger').toggle(res[id]<price && (price-res[id]>price/100));
});
});
}
gup.then(function(res) {
ajax.jsonRpc("/shop/cart/update_json", 'call', {
'line_id': line_id,
'product_id': parseInt($input.data('product-id'),10),
'set_qty': value})
.then(function (data) {
$input.data('update_change', false);
if (value !== parseInt($input.val(), 10)) {
$input.trigger('change');
return;
}
if (!data.quantity) {
location.reload(true);
return;
}
var $q = $(".my_cart_quantity");
$q.parent().parent().removeClass("hidden", !data.quantity);
$q.html(data.cart_quantity).hide().fadeIn(600);
$input.val(data.quantity);
$('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
$("#cart_total").replaceWith(data['website_sale.total']);
if (data.warning) {
var cart_alert = $('.oe_cart').parent().find('#data_warning');
if (cart_alert.length === 0) {
$('.oe_cart').prepend('<div class="alert alert-danger alert-dismissable" role="alert" id="data_warning">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> ' + data.warning + '</div>');
}
else {
cart_alert.html('<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> ' + data.warning);
}
$input.val(data.quantity);
ajax.jsonRpc("/shop/cart/update_json", 'call', {
'line_id': line_id,
'product_id': parseInt($input.data('product-id'),10),
'set_qty': value})
.then(function (data) {
$input.data('update_change', false);
if (value !== parseInt($input.val(), 10)) {
$input.trigger('change');
return;
}
var $q = $(".my_cart_quantity");
$q.parent().parent().removeClass("hidden", !data.quantity);
$q.html(data.cart_quantity).hide().fadeIn(600);
$input.val(data.quantity);
$('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
$(".js_cart_lines").first().before(data['website_sale.cart_lines']).end().remove();
if (data.warning) {
var cart_alert = $('.oe_cart').parent().find('#data_warning');
if (cart_alert.length === 0) {
$('.oe_cart').prepend('<div class="alert alert-danger alert-dismissable" role="alert" id="data_warning">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> ' + data.warning + '</div>');
}
});
else {
cart_alert.html('<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> ' + data.warning);
}
$input.val(data.quantity);
}
});
}, 500);
});
$(oe_website_sale).on("click", ".oe_cart a.js_add_suggested_products", function () {
$(this).prev('input').val(1).trigger('change');
});
// hack to add and rome from cart with json
$(oe_website_sale).on('click', 'a.js_add_cart_json', function (ev) {
ev.preventDefault();
+72 -68
View File
@@ -728,6 +728,71 @@
</xpath>
</template>
<template id="cart_lines" name="Shopping Cart">
<div t-if="not website_sale_order or not website_sale_order.website_order_line" class="js_cart_lines well well-lg">
Your cart is empty!
</div>
<table class="table table-striped table-condensed js_cart_lines" id="cart_products" t-if="website_sale_order and website_sale_order.website_order_line">
<thead>
<tr>
<th width="100">Product</th>
<th></th>
<th width="130" class="text-center">Quantity</th>
<th width="100" class="text-center">Price</th>
</tr>
</thead>
<tbody>
<t t-foreach="website_sale_order.website_order_line" t-as="line">
<tr>
<td colspan="2" t-if="not line.product_id.product_tmpl_id"></td>
<td align="center" t-if="line.product_id.product_tmpl_id">
<span t-field="line.product_id.image_small"
t-field-options='{"widget": "image", "class": "img-rounded"}'/>
</td>
<td t-if="line.product_id.product_tmpl_id">
<div>
<a t-attf-href="/shop/product/#{ slug(line.product_id.product_tmpl_id) }">
<strong t-esc="line.product_id.with_context(display_default_code=False).display_name"/>
</a>
</div>
<div class="text-muted" t-esc="'\n'.join(line.name.splitlines()[1:])"/>
</td>
<td class="text-center" id="td-qty">
<div class="input-group oe_website_spinner">
<a t-attf-href="#" class="mb8 input-group-addon js_add_cart_json" data-no-instant="">
<i class="fa fa-minus"></i>
</a>
<input type="text" class="js_quantity form-control"
t-att-data-line-id="line.id"
t-att-data-product-id="line.product_id.id"
t-att-value="int(line.product_uom_qty)"/>
<a t-attf-href="#" class="mb8 input-group-addon float_left js_add_cart_json" data-no-instant="">
<i class="fa fa-plus"></i>
</a>
</div>
</td>
<td class="text-center" id="td-price" name="price">
<del t-attf-class="text-danger #{compute_currency(line.product_id.lst_price) - line.price_reduce &lt; 1 and 'hidden' or ''}" style="text-decoration: line-through; white-space: nowrap;"
t-field="line.product_id.lst_price" t-field-options='{
"widget": "monetary",
"from_currency": "line.company_id.currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
<span t-field="line.price_reduce" style="white-space: nowrap;" t-field-options='{
"widget": "monetary",
"from_currency": "website_sale_order.pricelist_id.currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
</td>
</tr>
</t>
</tbody>
</table>
<div class="js_cart_lines pull-right">
<t t-call="website_sale.total"/>
</div>
</template>
<template id="cart" name="Shopping Cart">
<t t-call="website.layout">
<div id="wrap">
@@ -738,67 +803,9 @@
<h1 class="mb32">Shopping Cart</h1>
<div class="row">
<div class="col-md-8 col-sm-9 oe_cart">
<div t-if="not website_sale_order or not website_sale_order.website_order_line" class="well well-lg">
Your cart is empty!
</div>
<table class='table table-striped table-condensed' id="cart_products" t-if="website_sale_order and website_sale_order.website_order_line">
<thead>
<tr>
<th colspan="2" width="100">Product</th>
<th width="120">Quantity</th>
<th width="100">Price</th>
</tr>
</thead>
<tbody>
<t t-foreach="website_sale_order.website_order_line" t-as="line">
<tr>
<td colspan="2" t-if="not line.product_id.product_tmpl_id"></td>
<td align="center" t-if="line.product_id.product_tmpl_id">
<span t-field="line.product_id.image_small"
t-field-options='{"widget": "image", "class": "img-rounded"}'/>
</td>
<td t-if="line.product_id.product_tmpl_id">
<div>
<a t-attf-href="/shop/product/#{ slug(line.product_id.product_tmpl_id) }">
<strong t-esc="line.product_id.with_context(display_default_code=False).display_name"/>
</a>
</div>
<div class="text-muted" t-esc="'\n'.join(line.name.splitlines()[1:])"/>
</td>
<td class="text-center" id="td-qty">
<div class="input-group oe_website_spinner">
<a t-attf-href="#" class="mb8 input-group-addon js_add_cart_json" data-no-instant="">
<i class="fa fa-minus"></i>
</a>
<input type="text" class="js_quantity js_no_gup form-control"
t-att-data-line-id="line.id"
t-att-data-product-id="line.product_id.id"
t-att-value="int(line.product_uom_qty)"/>
<a t-attf-href="#" class="mb8 input-group-addon float_left js_add_cart_json" data-no-instant="">
<i class="fa fa-plus"></i>
</a>
</div>
</td>
<td class="text-center" id="td-price" name="price">
<del t-attf-class="text-danger mr8 #{compute_currency(line.product_id.lst_price) - line.price_reduce &lt; 1 and 'hidden' or ''}" style="text-decoration: line-through; white-space: nowrap;"
t-field="line.product_id.lst_price" t-field-options='{
"widget": "monetary",
"from_currency": "line.company_id.currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
<span t-field="line.price_reduce" style="white-space: nowrap;" t-field-options='{
"widget": "monetary",
"from_currency": "website_sale_order.pricelist_id.currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
</td>
</tr>
</t>
</tbody>
</table>
<div class="pull-right">
<t t-call="website_sale.total"/>
</div>
<t t-call="website_sale.cart_lines"/>
<div class="clearfix"/>
<a t-if="not optional_products and website_sale_order and website_sale_order.website_order_line" class="btn btn-primary pull-right mb32" href="/shop/checkout"><span class="hidden-xs">Process Checkout</span><span class="visible-xs-inline">Checkout</span> <span class="fa fa-long-arrow-right"/></a>
@@ -854,9 +861,9 @@
</t>
</template>
<template id="suggested_products_list" inherit_id="website_sale.cart" customize_show="True" name="Suggested Products in my cart">
<template id="suggested_products_list" inherit_id="website_sale.cart_lines" customize_show="True" name="Suggested Products in my cart">
<xpath expr="//table[@id='cart_products']" position="after">
<table t-if="suggested_products" class='table table-striped table-condensed'>
<table t-if="suggested_products" class='js_cart_lines table table-striped table-condensed'>
<colgroup>
<col width="80"/>
<col/>
@@ -900,11 +907,8 @@
}'/>
</td>
<td class="text-center">
<form action="/shop/cart/update" method="post">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<input name="product_id" t-att-value="product.id" type="hidden"/>
<a class="btn btn-link a-submit"><strong>Add to Cart</strong></a>
</form>
<input class="js_quantity" name="product_id" t-att-data-product-id="product.id" type="hidden"/>
<a class="btn btn-link js_add_suggested_products"><strong>Add to Cart</strong></a>
</td>
</tr>
</tbody>
@@ -89,18 +89,6 @@ $('.oe_website_sale #add_to_cart, .oe_website_sale #products_grid .a-submit')
product_ids.push(values[0]);
});
});
ajax.jsonRpc("/shop/get_unit_price", 'call', {'product_ids': product_ids, 'add_qty': parseInt(qty)})
.then(function (data) {
for(var i=0; i < $products_dom.length; i++) {
var current = $products_dom[i].data("attribute_value_ids");
for(var j=0; j < current.length; j++){
current[j][2] = data[current[j][0]];
}
$products_dom[i].attr("data-attribute_value_ids", JSON.stringify(current)).trigger("change");
}
$dom.find(".oe_price .oe_currency_value").text(data[product_id].toFixed(2));
$dom.find('.text-danger.oe_default_price').toggle(data[product_id]<default_price && (default_price-data[product_id]>default_price/100)).css('text-decoration', 'line-through');
});
});
});
return false;
@@ -10,7 +10,7 @@
</xpath>
</template>
<template id="optional_products" inherit_id="website_sale.cart" name="Optional products">
<template id="optional_products" inherit_id="website_sale.cart_lines" name="Optional products">
<xpath expr="//table[@id='cart_products']/tbody//td[@id='td-qty']/div" position="attributes">
<attribute name="t-if">not line.linked_line_id</attribute>
</xpath>