Skip to content

Commit 35ea9bf

Browse files
committed
[FIX] website_sale, delivery: check carrier before payment
Check if the user selected the carrier for storable/consumable products before proceeding to payment. opw-3810367 closes #161476 Related: odoo/enterprise#60550 Signed-off-by: Valeriya Chuprina (vchu) <vchu@odoo.com>
1 parent 7d2b1e2 commit 35ea9bf

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

addons/delivery/models/sale_order.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def _remove_delivery_line(self):
5454
+ '\n'.join(['- %s: %s x %s' % (line.product_id.with_context(display_default_code=False).display_name, line.qty_invoiced, line.price_unit) for line in delivery_lines])
5555
)
5656
to_delete.unlink()
57+
self.carrier_id = self.env['delivery.carrier'] # reset carrier
5758

5859
def set_delivery_line(self, carrier, amount):
5960
self._remove_delivery_line()

addons/website_sale/controllers/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,9 @@ def shop_payment_transaction(self, order_id, access_token, **kwargs):
18621862
if tools.float_compare(kwargs['amount'], order_sudo.amount_total, precision_rounding=order_sudo.currency_id.rounding):
18631863
raise ValidationError(_("The cart has been updated. Please refresh the page."))
18641864

1865+
if not order_sudo.only_services and not order_sudo.carrier_id:
1866+
raise ValidationError(_("No shipping method is selected."))
1867+
18651868
tx_sudo = self._create_transaction(
18661869
custom_create_values={'sale_order_ids': [Command.set([order_id])]}, **kwargs,
18671870
)

addons/website_sale/i18n/website_sale.pot

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,13 @@ msgid ""
25352535
" Please contact us for more information."
25362536
msgstr ""
25372537

2538+
#. module: website_sale
2539+
#. odoo-python
2540+
#: code:addons/website_sale/controllers/main.py:0
2541+
#, python-format
2542+
msgid "No shipping method is selected."
2543+
msgstr ""
2544+
25382545
#. module: website_sale
25392546
#: model:ir.model.fields.selection,name:website_sale.selection__website__product_page_image_spacing__none
25402547
#: model_terms:ir.ui.view,arch_db:website_sale.snippet_options

addons/website_sale/tests/test_website_sale_cart.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from odoo.addons.website_sale.controllers.main import WebsiteSale, PaymentPortal
77
from odoo.addons.website.tools import MockRequest
88
from odoo.addons.website_sale.models.product_template import ProductTemplate
9-
from odoo.exceptions import UserError
9+
from odoo.exceptions import UserError, ValidationError
1010
from odoo.tests.common import tagged
1111
from odoo.fields import Command
1212

@@ -20,6 +20,13 @@ def setUpClass(cls):
2020
cls.website = cls.env['website'].browse(1)
2121
cls.WebsiteSaleController = WebsiteSale()
2222
cls.public_user = cls.env.ref('base.public_user')
23+
cls.product = cls.env['product.product'].create({
24+
'name': 'Test Product',
25+
'sale_ok': True,
26+
'website_published': True,
27+
'lst_price': 1000.0,
28+
'standard_price': 800.0,
29+
})
2330

2431
def test_add_cart_deleted_product(self):
2532
# Create a published product then unlink it
@@ -101,24 +108,29 @@ def test_zero_price_product_rule(self):
101108
self.WebsiteSaleController.cart_update_json(product_id=product_service.id, add_qty=1)
102109

103110
def test_update_cart_before_payment(self):
104-
product = self.env['product.product'].create({
105-
'name': 'Test Product',
106-
'sale_ok': True,
107-
'website_published': True,
108-
'lst_price': 1000.0,
109-
'standard_price': 800.0,
110-
})
111111
website = self.website.with_user(self.public_user)
112-
with MockRequest(product.with_user(self.public_user).env, website=website):
113-
self.WebsiteSaleController.cart_update_json(product_id=product.id, add_qty=1)
112+
with MockRequest(self.product.with_user(self.public_user).env, website=website):
113+
self.WebsiteSaleController.cart_update_json(product_id=self.product.id, add_qty=1)
114114
sale_order = website.sale_get_order()
115115
sale_order.access_token = 'test_token'
116116
old_amount = sale_order.amount_total
117-
self.WebsiteSaleController.cart_update_json(product_id=product.id, add_qty=1)
117+
self.WebsiteSaleController.cart_update_json(product_id=self.product.id, add_qty=1)
118118
# Try processing payment with the old amount
119119
with self.assertRaises(UserError):
120120
PaymentPortal().shop_payment_transaction(sale_order.id, sale_order.access_token, amount=old_amount)
121121

122+
def test_check_order_delivery_before_payment(self):
123+
website = self.website.with_user(self.public_user)
124+
with MockRequest(self.product.with_user(self.public_user).env, website=website):
125+
sale_order = self.env['sale.order'].create({
126+
'partner_id': self.public_user.id,
127+
'order_line': [Command.create({'product_id': self.product.id})],
128+
'access_token': 'test_token',
129+
})
130+
# Try processing payment with a storable product and no carrier_id
131+
with self.assertRaises(ValidationError):
132+
PaymentPortal().shop_payment_transaction(sale_order.id, sale_order.access_token)
133+
122134
def test_update_cart_zero_qty(self):
123135
# Try to remove a product that has already been removed
124136
product = self.env['product.product'].create({

0 commit comments

Comments
 (0)