[FIX] website_sale: recalculate shipping price on address change

Steps to reproduce:
1) Configure a carrier with 3d party api(for ex. fedex)
2) Create 2 shipping addresses(better to choose addresses with different
delivery rates)
3) Go to /shop and add product that needs to be delivered
4) Proceed to checkout choosing a shipping address and a carrier
5) See the calculated rate
6) Click on 'edit' near the addresses and change the address
7) Click 'confirm' and observe that a new rate on the badge is not
applied to the order

After this commit the rate is recalculated and updated when the shipment
address is changed.

opw-3737266

closes odoo/odoo#155943

X-original-commit: a6288931be85a7771ea05c8dc5bc2dbcaf8f71a4
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
Signed-off-by: Valeriya Chuprina (vchu) <vchu@odoo.com>
This commit is contained in:
Valeriya(vchu)
2024-03-04 08:11:58 +00:00
parent 7d27c255aa
commit 760bd7c955
2 changed files with 43 additions and 0 deletions
+3
View File
@@ -1595,6 +1595,9 @@ class WebsiteSale(payment_portal.PaymentPortal):
and partner_sudo != order_sudo.partner_shipping_id
):
order_sudo.partner_shipping_id = partner_id
if order_sudo.carrier_id:
# update carrier rates on shipping address change
order_sudo._check_carrier_quotation(force_carrier_id=order_sudo.carrier_id.id)
else:
# TODO someday we should gracefully handle invalid addresses
return
@@ -2,6 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
from unittest.mock import patch
from werkzeug.exceptions import Forbidden
@@ -297,6 +298,45 @@ class TestWebsiteSaleCheckoutAddress(TransactionCaseWithUserDemo, HttpCaseWithUs
self.WebsiteSaleController.address(**self.default_billing_address_values)
self.assertEqual(new_partner.company_id, self.website.company_id, "Public user edited billing (the partner itself) should not get its company modified.")
def test_03_carrier_rate_on_shipping_address_change(self):
""" Test that when a shipping address is changed the price of delivery is recalculated
and updated on the order."""
# Create a dummy carrier.
dummy_delivery_product = self.env['product.product'].create({
'name': 'Dummy',
'type': 'service',
'categ_id': self.env.ref('delivery.product_category_deliveries').id,
})
carrier = self.env['delivery.carrier'].create({
'name': 'Fixed',
'product_id': dummy_delivery_product.id,
'website_published': True,
})
partner = self.env.user.partner_id
order = self._create_so(partner.id)
order.carrier_id = carrier.id # Set the carrier on the order.
shipping_partner_values = {'name': 'dummy', 'parent_id': partner.id, 'type': 'delivery'}
shipping_partner = self.env['res.partner'].create(shipping_partner_values)
with MockRequest(self.env, website=self.website, sale_order_id=order.id):
with patch(
'odoo.addons.delivery.models.delivery_carrier.DeliveryCarrier.rate_shipment',
return_value={'success': True, 'price': 10, 'warning_message': ''}
) as rate_shipment_mock:
# Change a shipping address of the order in the checkout.
self.WebsiteSaleController.update_cart_address(
partner_id=shipping_partner.id, mode='shipping'
)
self.assertGreaterEqual(
rate_shipment_mock.call_count,
1,
msg="The carrier rate must be recalculated when shipping address is changed.",
)
self.assertEqual(
order.order_line.filtered(lambda l: l.is_delivery)[0].price_unit,
10,
msg="The recalculated delivery price must be updated on the order.",
)
def test_04_apply_empty_pl(self):
''' Ensure empty pl code reset the applied pl '''
so = self._create_so(self.env.user.partner_id.id)