[FIX] website_sale_options: keep website language on cart addition

In the ecommerce, when adding a product to the cart
while having website_sale_options installed
the product was added in the cart within the
website default language, not in the current
language of the visitor. The description of the product
was in the default website language (for instance, English)
instead of being in the visitor language (for instance, French).

The reason is quite simple: With website_sale_options, routes
are called in javascript, and these calls do not include the
website language within the url to the route
(e.g., call to '/shop/modal' instead of '/fr_FR/shop/modal)
and the language in the request context is therefore
the website default language.

The solution proposed here is probably not the cleanest possible,
a cleaner solution would be to define a new utility
JS function within website javascript to perform
Ajax calls, automatically adding the language to the url path
according to the current visitor language.

Another solution would be to set the lang of the session context
to the visitor language, and to use this lang instead of the
lang within request.context.

Nevertheless, none of the two above solutions can be performed in
stable releases, such as 8.0, to avoid any risks.

opw-631400
This commit is contained in:
Denis Ledoux 2015-03-25 14:23:12 +01:00
parent 287b293283
commit 0cf87d1671
2 changed files with 8 additions and 2 deletions

View File

@ -23,8 +23,11 @@ class website_sale_options(website_sale):
return r
@http.route(['/shop/cart/update_option'], type='http', auth="public", methods=['POST'], website=True)
def cart_options_update_json(self, product_id, add_qty=1, set_qty=0, goto_shop=None, **kw):
def cart_options_update_json(self, product_id, add_qty=1, set_qty=0, goto_shop=None, lang=None, **kw):
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
if lang:
context = dict(context, lang=lang)
request.website = request.website.with_context(context)
order = request.website.sale_get_order(force_create=1)
product = pool['product.product'].browse(cr, uid, int(product_id), context=context)
@ -57,11 +60,13 @@ class website_sale_options(website_sale):
if not context.get('pricelist'):
context['pricelist'] = int(pricelist)
website_context = kw.get('kwargs', {}).get('context', {})
context = dict(context or {}, **website_context)
from_currency = pool.get('product.price.type')._get_field_currency(cr, uid, 'list_price', context)
to_currency = pricelist.currency_id
compute_currency = lambda price: pool['res.currency']._compute(cr, uid, from_currency, to_currency, price, context=context)
product = pool['product.product'].browse(cr, uid, int(product_id), context=context)
request.website = request.website.with_context(context)
return request.website._render("website_sale_options.modal", {
'product': product,

View File

@ -27,6 +27,7 @@ $(document).ready(function () {
var $a = $(this);
$form.ajaxSubmit({
url: '/shop/cart/update_option',
data: {lang: openerp.website.get_context().lang},
success: function (quantity) {
if (!$a.hasClass('js_goto_shop')) {
window.location.href = window.location.href.replace(/shop([\/?].*)?$/, "shop/cart");