[IMP] website_sale: display the discount in the product template when the user select a variant

This commit is contained in:
Christophe Matthieu 2014-06-19 10:53:30 +02:00 committed by Christophe Simonis
parent 40840436f4
commit 22d528c7a7
4 changed files with 43 additions and 18 deletions

View File

@ -202,11 +202,12 @@ class website_sale(http.Controller):
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
category_obj = pool['product.public.category']
template_obj = pool['product.template']
currency_obj = pool['res.currency']
context.update(active_id=product.id)
if category:
category = category_obj.browse(request.cr, request.uid, int(category), context=request.context)
category = category_obj.browse(cr, uid, int(category), context=context)
attrib_list = request.httprequest.args.getlist('attrib')
attrib_values = [map(int,v.split("-")) for v in attrib_list if v]
@ -220,13 +221,23 @@ class website_sale(http.Controller):
if not context.get('pricelist'):
context['pricelist'] = int(self.get_pricelist())
product = template_obj.browse(request.cr, request.uid, int(product), context=context)
product = template_obj.browse(cr, uid, int(product), context=context)
optional_product_ids = []
for p in product.optional_product_ids:
ctx = dict(context, active_id=p.id)
optional_product_ids.append(template_obj.browse(cr, uid, p.id, context=ctx))
attribute_value_ids = []
if request.website.company_pricelist_id.id != context['pricelist']:
company_currency_id = request.website.company_currency_id.id
currency_id = self.get_pricelist().currency_id.id
for p in product.product_variant_ids:
price = currency_obj.compute(cr, uid, company_currency_id, currency_id, p.lst_price)
attribute_value_ids.append([p.id, map(int, p.attribute_value_ids), p.price, price])
else:
attribute_value_ids = [[p.id, map(int, p.attribute_value_ids), p.price, p.lst_price] for p in product.product_variant_ids]
values = {
'search': search,
'category': category,
@ -237,7 +248,8 @@ class website_sale(http.Controller):
'category_list': category_list,
'main_object': product,
'product': product,
'optional_product_ids': optional_product_ids
'optional_product_ids': optional_product_ids,
'attribute_value_ids': attribute_value_ids
}
return request.website.render("website_sale.product", values)

View File

@ -171,7 +171,9 @@ class website(orm.Model):
_columns = {
'pricelist_id': fields.related('user_id','partner_id','property_product_pricelist',
type='many2one', relation='product.pricelist', string='Default pricelist'),
'company_currency_id': fields.related('company_id','partner_id','property_product_pricelist','currency_id',
'company_pricelist_id': fields.related('company_id','partner_id','property_product_pricelist',
type='many2one', relation='product.pricelist', string='Default pricelist'),
'company_currency_id': fields.related('company_pricelist_id','currency_id',
type='many2one', relation='res.currency', string='Default pricelist'),
}

View File

@ -76,11 +76,18 @@ $(document).ready(function () {
$('.css_attribute_color:has(input:checked)').addClass("active");
});
function price_to_str(price) {
price = Math.round(price * 100) / 100;
var dec = Math.round((price % 1) * 100);
return price + (dec ? '' : '.0') + (dec%10 ? '' : '0');
}
$('input.js_variant_change, select.js_variant_change').change(function (ev) {
var $ul = $(this).parents('ul.js_add_cart_variants:first');
var $parent = $ul.parents('.js_product:first');
var $porduct_id = $parent.find('input.product_id, input.optional_product_id').first();
var $price = $parent.find(".oe_price .oe_currency_value:first");
var $price = $parent.find(".oe_price:first .oe_currency_value");
var $default_price = $parent.find(".oe_default_price:first .oe_currency_value");
var variant_ids = $ul.data("attribute_value_ids");
var values = [];
$parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () {
@ -92,8 +99,9 @@ $(document).ready(function () {
var product_id = false;
for (var k in variant_ids) {
if (_.isEqual(variant_ids[k][1], values)) {
var dec = Math.round((variant_ids[k][2] % 1) * 100);
$price.html(variant_ids[k][2] + (dec ? '' : '.0') + (dec%10 ? '' : '0'));
$price.html(price_to_str(variant_ids[k][2]));
$default_price.html(price_to_str(variant_ids[k][3]));
$default_price.parent().toggle(variant_ids[k][3]-variant_ids[k][2]>0.2);
product_id = variant_ids[k][0];
break;
}

View File

@ -430,19 +430,18 @@
</div>
</td>
<td>
<t t-if="(product.lst_price != product.price) &gt; 0.1">
<span class="text-danger" style="text-decoration: line-through; white-space: nowrap;"
<span t-attf-class="text-danger oe_default_price" t-att-style="'' if (website.compute_curency(product.lst_price) - product.price) &gt; 0.1 else 'display: none;'" style="text-decoration: line-through; white-space: nowrap;"
t-field="product.lst_price"
t-field-options='{
"widget": "monetary",
"from_currency": "website.company_currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/><br/>
</t>
<span class="oe_price" style="white-space: nowrap;"
t-field="product.price"
t-field-options='{
"widget": "monetary",
"from_currency": "website.company_currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
</td>
@ -569,15 +568,13 @@
<template id="product_price">
<div itemprop="offers" itemscope="itemscope" itemtype="http://schema.org/Offer" class="product_price mt16">
<h4 class="oe_price_h4">
<t t-if="(website.compute_curency(product.lst_price) - product.price) &gt; 0.1">
<span class="text-danger" style="text-decoration: line-through; white-space: nowrap;"
<span class="text-danger oe_default_price" t-att-style="'' if (website.compute_curency(product.lst_price) - product.price) &gt; 0.1 else 'display: none;'" style="text-decoration: line-through; white-space: nowrap;"
t-field="product.lst_price"
t-field-options='{
"widget": "monetary",
"from_currency": "website.company_currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/><br/>
</t>
<b class="oe_price" style="white-space: nowrap;"
t-field="product.price"
t-field-options='{
@ -598,7 +595,11 @@
<input type="radio" name="product_id" t-att-value="variant_id.id"/>
<span t-esc="variant_id.name_get()[0][1]"/>
<span class="badge" t-if="variant_id.price_extra">
<t t-esc="variant_id.price_extra > 0 and '+' or ''"/><span t-field="variant_id.price_extra" style="white-space: nowrap;" t-field-options='{ "widget": "monetary", "display_currency": "pricelist.currency_id" }'/>
<t t-esc="variant_id.price_extra > 0 and '+' or ''"/><span t-field="variant_id.price_extra" style="white-space: nowrap;" t-field-options='{
"widget": "monetary",
"from_currency": "website.company_currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
</span>
</label>
</t>
@ -606,7 +607,7 @@
</template>
<template id="variants">
<ul t-attf-class="list-unstyled js_add_cart_variants #{ul_class}" t-att-data-attribute_value_ids="[[p.id, map(int, p.attribute_value_ids), p.price] for p in product.product_variant_ids]">
<ul t-attf-class="list-unstyled js_add_cart_variants #{ul_class}" t-att-data-attribute_value_ids="attribute_value_ids">
<t t-foreach="product.attribute_line_ids" t-as="variant_id">
<li t-if="len(variant_id.value_ids) > 1">
@ -620,6 +621,7 @@
<span t-if="value_id.price_extra">
<t t-esc="value_id.price_extra > 0 and '+' or ''"/><span t-field="value_id.price_extra" style="white-space: nowrap;" t-field-options='{
"widget": "monetary",
"from_currency": "website.company_currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
</span>
@ -638,9 +640,10 @@
<span t-field="value_id.name"/>
<span class="badge" t-if="value_id.price_extra">
<t t-esc="value_id.price_extra > 0 and '+' or ''"/><span t-field="value_id.price_extra" style="white-space: nowrap;" t-field-options='{
"widget": "monetary",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
"widget": "monetary",
"from_currency": "website.company_currency_id",
"display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
}'/>
</span>
</label>
</li>