Skip to Content
Menu
This question has been flagged

Hello,

i am using Fedex as Shipping carrier in ODOO14. Fedex has different services and prices are different. I am trying to write a phyton code when you click on Delivery order to check the shipping rates for all Fedex Service set-up and choose the cheapest one.

The same you can do manually if you click on a Sales order the "Add Shipment" button and then "Get Rate".


I have written this phython code. But it does not work at all. I am not sure which function is used when you click on the button 'Get Rate' on the Sales order. So it's more a guess:


def find_cheapest_fedex(self, order):

fedex_carriers = self.env['delivery.carrier'].search([('delivery_type', '=', 'fedex')])

cheapest_rate = float('inf')

cheapest_carrier = None


for carrier in fedex_carriers:

result = carrier.rate_shipment(order)

if result['success']:

price = result['price']

if price < cheapest_rate:

cheapest_rate = price

cheapest_carrier = carrier


if cheapest_carrier:

order.write({

'carrier_id': cheapest_carrier.id,

'delivery_price': cheapest_rate,

})

return True

else:

return False # or handle this case as appropriate for your needs


Thanks for any help,

Dan


Avatar
Discard
Related Posts Replies Views Activity
1
Nov 20
4271
2
May 25
879
0
Feb 24
1063
2
Sep 23
1518
1
May 23
1885