From a1944aa4addfeda93bcc67719b251d566fb5a5ec Mon Sep 17 00:00:00 2001 From: "Eteil Djoumatchoua(etdj)" Date: Mon, 24 Jul 2023 08:04:22 +0000 Subject: [PATCH] [FIX] website_sale_loyalty: Stop getting succes message after applying invalid coupon Current Behaviour: In E-commerce, when you apply a ``LoyaltyCard`` which is not applicable on the ``SaleOrder``, in a specific case it displays a success message even if the coupon is not applied. Expected behaviour Don't display a success message when the coupon is not applicable Steps: - Create a program with 'coupon' type, a rule of 'minimum 1' and a reward on a specific product A. - Generate a coupon - Go to shop add product A to the cart, go to checkout and apply the coupon code(Everything goes well). - Delete the coupon and the product, and add a product B. - Go to checkout and try to apply the same coupon code. Issue: Wrong success message Reason: The wrong message is displayed beacause on the python side we just check if there is 'error' in the ``Request.session`` but in this case there is also 'error_promo_code' that should be considered. opw-3355632 closes odoo/odoo#129663 X-original-commit: 6ee60a30e0798ce69152385b08e49b223782d785 Signed-off-by: Victor Feyens (vfe) Signed-off-by: Eteil Junior Djoumatchoua (etdj) --- .../website_sale_loyalty/controllers/main.py | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/addons/website_sale_loyalty/controllers/main.py b/addons/website_sale_loyalty/controllers/main.py index ba1be552ac0..30a66c0a3d5 100644 --- a/addons/website_sale_loyalty/controllers/main.py +++ b/addons/website_sale_loyalty/controllers/main.py @@ -15,17 +15,19 @@ class WebsiteSale(main.WebsiteSale): def pricelist(self, promo, **post): order = request.website.sale_get_order() coupon_status = order._try_apply_code(promo) - if 'error' not in coupon_status: - if len(coupon_status) == 1: - coupon, rewards = next(iter(coupon_status.items())) - if len(rewards) == 1 and not rewards.multi_product: - self._apply_reward(order, rewards, coupon) if coupon_status.get('not_found'): return super(WebsiteSale, self).pricelist(promo, **post) elif coupon_status.get('error'): request.session['error_promo_code'] = coupon_status['error'] - if 'error' not in coupon_status: - request.session['successful_code'] = promo + elif 'error' not in coupon_status: + reward_successfully_applied = True + if len(coupon_status) == 1: + coupon, rewards = next(iter(coupon_status.items())) + if len(rewards) == 1 and not rewards.multi_product: + reward_successfully_applied = self._apply_reward(order, rewards, coupon) + + if reward_successfully_applied: + request.session['successful_code'] = promo return request.redirect(post.get('r', '/shop/cart')) @http.route() @@ -102,10 +104,20 @@ class WebsiteSale(main.WebsiteSale): return request.redirect(redirect) def _apply_reward(self, order, reward, coupon): + """Try to apply the given program reward + + :returns: whether the reward was successfully applied + :rtype: bool + """ try: - order._apply_program_reward(reward, coupon) + reward_status = order._apply_program_reward(reward, coupon) except UserError as e: request.session['error_promo_code'] = str(e) + return False + if 'error' in reward_status: + request.session['error_promo_code'] = reward_status['error'] + return False + return True @http.route() def cart_update_json(self, *args, set_qty=None, **kwargs):