Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FW][FIX] website_sale_loyalty: allow to remove coupon from cart #156370

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions addons/website_sale_loyalty/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def get_promo_code_success_message(self, delete=True):
return code

def _cart_update(self, *args, **kwargs):
product_id, set_qty = kwargs['product_id'], kwargs.get('set_qty')

line = self.order_line.filtered(lambda l: l.product_id.id == product_id)
reward_id = line.reward_id
if set_qty == 0 and line.coupon_id and reward_id and reward_id.reward_type == 'discount':
# Force the deletion of the line even if it's a temporary record created by new()
kwargs['line_id'] = line.id

res = super(SaleOrder, self)._cart_update(*args, **kwargs)
self._update_programs_and_rewards()
self._auto_apply_rewards()
Expand Down
28 changes: 28 additions & 0 deletions addons/website_sale_loyalty/tests/test_shop_sale_coupon.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,31 @@ def test_01_gc_coupon(self):
order._gc_abandoned_coupons()

self.assertEqual(len(order.applied_coupon_ids), 0, "The coupon should've been removed from the order as more than 4 days")

def test_02_remove_coupon(self):
# 1. Simulate a frontend order (website, product)
order = self.empty_order
order.website_id = self.env['website'].browse(1)
self.env['sale.order.line'].create({
'product_id': self.env['product.product'].create({
'name': 'Product A', 'list_price': 100, 'sale_ok': True
}).id,
'name': 'Product A',
'order_id': order.id,
})

# 2. Apply the coupon
self._apply_promo_code(order, self.coupon.code)

# 3. Remove the coupon
coupon_line = order.website_order_line.filtered(
lambda l: l.coupon_id and l.coupon_id.id == self.coupon.id
)

kwargs = {
'line_id': None, 'product_id': coupon_line.product_id.id, 'add_qty': None, 'set_qty': 0
}
order._cart_update(**kwargs)

msg = "The coupon should've been removed from the order"
self.assertEqual(len(order.applied_coupon_ids), 0, msg=msg)