From 7bb7cdcb98f0f4c1bb80aeef41ee85302dd8c573 Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Tue, 18 Apr 2023 08:09:53 +0000 Subject: [PATCH] [FIX] website_sale: clear old pricelist in the session On the ecommerce, we want the pricelist to be adapted to the user who is connected. If no user is logged in, the 'Public Pricelist' is used. To determine the pricelist that will be used (by default): https://github.com/odoo/odoo/blob/a3169ede4f609b56c83da04756d20b1c7a07251f/addons/website_sale/controllers/main.py#L350-L354 Therefore, if we have been to the shop as a 'Public user' and we decide to log in, we already have a pricelist in `request.session.get('website_sale_current_pl')`. So we have to clear the pricelist when we connect to determine the new pricelist of the user. opw-3228998 closes odoo/odoo#121074 X-original-commit: 71b2d817df93f0cc8c1f5c424a3555bcadf33b2d Signed-off-by: Lefebvre Thomas (thle) --- addons/website_sale/controllers/main.py | 7 ++++ .../tours/website_sale_shop_pricelist_tour.js | 37 +++++++++++++++++++ .../tests/test_website_sale_pricelist.py | 33 +++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 addons/website_sale/static/tests/tours/website_sale_shop_pricelist_tour.js diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index c8a5de9e0b4..c63f2c5e242 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -126,6 +126,13 @@ class WebsiteSaleForm(WebsiteForm): class Website(main.Website): + + def _login_redirect(self, uid, redirect=None): + # If we are logging in, clear the current pricelist to be able to find + # the pricelist that corresponds to the user afterwards. + request.session.pop('website_sale_current_pl', None) + return super()._login_redirect(uid, redirect=redirect) + @http.route() def autocomplete(self, search_type=None, term=None, order=None, limit=5, max_nb_chars=999, options=None): options = options or {} diff --git a/addons/website_sale/static/tests/tours/website_sale_shop_pricelist_tour.js b/addons/website_sale/static/tests/tours/website_sale_shop_pricelist_tour.js new file mode 100644 index 00000000000..6b5c105be72 --- /dev/null +++ b/addons/website_sale/static/tests/tours/website_sale_shop_pricelist_tour.js @@ -0,0 +1,37 @@ +/** @odoo-module **/ + +import { registry } from "@web/core/registry"; + +registry.category("web_tour.tours").add( + "website_sale.website_sale_shop_pricelist_tour", + { + test: true, + url: '/shop', + steps: [ + { + content: "Check pricelist", + trigger: ".o_pricelist_dropdown .dropdown-toggle:not(:contains('User Pricelist'))", + run: function() {} // Check + }, + { + content: "Go to login page", + trigger: ".nav-link:contains('Sign in')" + }, + { + content: "Submit login", + trigger: '.oe_login_form', + run: function () { + $('.oe_login_form input[name="login"]').val("toto"); + $('.oe_login_form input[name="password"]').val("long_enough_password"); + $('.oe_login_form input[name="redirect"]').val("/shop"); + $('.oe_login_form').submit(); + } + }, + { + content: "Check pricelist", + trigger: ".o_pricelist_dropdown .dropdown-toggle:contains('User Pricelist')", + run: function() {} // Check + }, + ] + } +); diff --git a/addons/website_sale/tests/test_website_sale_pricelist.py b/addons/website_sale/tests/test_website_sale_pricelist.py index 9a95da7e483..92aa3b50126 100644 --- a/addons/website_sale/tests/test_website_sale_pricelist.py +++ b/addons/website_sale/tests/test_website_sale_pricelist.py @@ -599,3 +599,36 @@ class TestWebsitePriceListMultiCompany(TransactionCaseWithUserDemo): # The test is here: while having access only to self.company2 records, # archive should not raise an error self.c2_pl.with_user(self.demo_user).with_context(allowed_company_ids=self.company2.ids).write({'active': False}) + +@tagged('post_install', '-at_install') +class TestWebsiteSaleSession(HttpCaseWithUserPortal): + + def test_update_pricelist_user_session(self): + """ + The objective is to verify that the pricelist + changes correctly according to the user. + """ + website = self.env.ref('website.default_website') + test_user = self.env['res.users'].create({ + 'name': 'Toto', + 'login': 'toto', + 'password': 'long_enough_password', + }) + user_pricelist, _ = self.env['product.pricelist'].create([ + { + 'name': 'User Pricelist', + 'website_id': website.id, + 'code': 'User_pricelist', + 'selectable': True, + 'sequence': 40, # Be sure not to use it by default + }, + { + 'name': 'Other Pricelist', + 'website_id': website.id, + 'code': 'Other_pricelist', + 'selectable': True, + 'sequence': 30, + } + ]) + test_user.partner_id.property_product_pricelist = user_pricelist + self.start_tour("/shop", 'website_sale.website_sale_shop_pricelist_tour', login="")