From cf16b96ae036957e3f33ad614a1512f66840ed53 Mon Sep 17 00:00:00 2001 From: mega Date: Tue, 23 Apr 2024 18:01:03 +0530 Subject: [PATCH] [FIX] sale_loyalty: prevent error while remove all product quantities from cart Currently, an error is generated when removing all product quantities from the cart after a claiming a reward(discount). Step to produce: - Install a 'website_sale_loyalty' module. - Navigate to the website / eCommerce / Loyalty / Discount & Loyalty to create a record. - Set the Loyalty Program name and Program Type as 'Loyalty Cards'.(Ensure it's available on sale and the website.) - And add 'Rewards' and set a Reward Type as 'Discount' which is applied to on Cheapest Product. - Go to the website shop add any product on a card, Open a cart increase the quantity of the product, and claim the discount reward. - Again go to Loyalty Program and open Loyalty Card, Open a record and add a Balance(greater than 200 as default reward points are 200) and copy 'Code'. - Again go to the website shop and apply this code to claim a discount after a claim discount. - Now remove all product quantity from a cart. AttributeError: 'bool' object has no attribute 'price_unit' The issue occurs when attempting to remove all product quantities from a cart. At this point [1], a bool value 'False' is returned, and the system attempts to get a value of 'price_unit' from it [2]. link [1]: https://github.com/odoo/odoo/blob/499056a82db26f7d9caa86314e666e2bd49cc79c/addons/sale_loyalty/models/sale_order.py#L187-L195 link [2]: https://github.com/odoo/odoo/blob/499056a82db26f7d9caa86314e666e2bd49cc79c/addons/sale_loyalty/models/sale_order.py#L205 This commit resolve issue, If the _cheapest_line() method returns False then also returns False from _discountable_cheapest(), To raise an error at [3]. link [3]: https://github.com/odoo/odoo/blob/cbc40eccf576c499709f7825edad9a3b3ce7a22d/addons/sale_loyalty/models/sale_order.py#L317-L333 sentry-5119007021 closes odoo/odoo#163403 X-original-commit: dfd1aabb40d8c8e6e80f1b194a4b61e1dfb41608 Signed-off-by: Meet Gandhi (mega) --- addons/sale_loyalty/models/sale_order.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/addons/sale_loyalty/models/sale_order.py b/addons/sale_loyalty/models/sale_order.py index 7357001f83d..a0ed2161f11 100644 --- a/addons/sale_loyalty/models/sale_order.py +++ b/addons/sale_loyalty/models/sale_order.py @@ -208,6 +208,8 @@ class SaleOrder(models.Model): assert reward.discount_applicability == 'cheapest' cheapest_line = self._cheapest_line() + if not cheapest_line: + return False, False discountable = cheapest_line.price_total discountable_per_taxes = cheapest_line.price_unit * (1 - (cheapest_line.discount or 0) / 100) taxes = cheapest_line.tax_id.filtered(lambda t: t.amount_type != 'fixed')