[FIX] website_sale: do not create zero-quantity lines

We do not want to allow the creation of lines with zero quantity,
Although we do not know of a standard flow making such requests,
the cart_update routes could accept information such as
* {"product_id": X, "set_qty":0}
* {"product_id": X, "add_qty":0}

If a line with product X already exists, it'll be deleted.
If no such line exists, a new one would be created, which is not
something we want to support.

closes odoo/odoo#118999

X-original-commit: 231c8aa79ddd0b1f4ed47508fa22596ac19daacb
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
Co-authored-by: Paul Morelle <pmo@odoo.com>
This commit is contained in:
Victor Feyens
2023-04-21 15:02:15 +02:00
co-authored by Paul Morelle
parent a216ee0bc3
commit cbd67bd099
2 changed files with 33 additions and 1 deletions
+1 -1
View File
@@ -132,7 +132,7 @@ class SaleOrder(models.Model):
update_values = self._prepare_order_line_update_values(order_line, quantity, **kwargs)
if update_values:
self._update_cart_line_values(order_line, update_values)
elif quantity >= 0:
elif quantity > 0:
# Create new line
order_line_values = self._prepare_order_line_values(product_id, quantity, **kwargs)
order_line = self.env['sale.order.line'].sudo().create(order_line_values)
@@ -78,3 +78,35 @@ class WebsiteSaleCart(TransactionCase):
# Try processing payment with the old amount
with self.assertRaises(UserError):
PaymentPortal().shop_payment_transaction(sale_order.id, sale_order.access_token, amount=old_amount)
def test_update_cart_zero_qty(self):
# Try to remove a product that has already been removed
product = self.env['product.product'].create({
'name': 'Test Product',
'sale_ok': True,
'website_published': True,
'lst_price': 1000.0,
'standard_price': 800.0,
})
portal_user = self.env.ref('base.demo_user0')
website = self.website.with_user(portal_user)
SaleOrderLine = self.env['sale.order.line']
with MockRequest(product.with_user(portal_user).env, website=website):
# add the product to the cart
self.WebsiteSaleController.cart_update_json(product_id=product.id, add_qty=1)
sale_order = website.sale_get_order()
self.assertEqual(sale_order.amount_total, 1150.0)
# remove the product from the cart
self.WebsiteSaleController.cart_update_json(product_id=product.id, line_id=sale_order.order_line.id, set_qty=0)
self.assertEqual(sale_order.amount_total, 0.0)
self.assertEqual(sale_order.order_line, SaleOrderLine)
# removing the product again doesn't add a line with zero quantity
self.WebsiteSaleController.cart_update_json(product_id=product.id, set_qty=0)
self.assertEqual(sale_order.order_line, SaleOrderLine)
self.WebsiteSaleController.cart_update_json(product_id=product.id, add_qty=0)
self.assertEqual(sale_order.order_line, SaleOrderLine)