Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
5997 Lượt xem

Ive created some delivery methods (from Inventory > Configuration > Shipping methods) which I want to assign to my sale order records. Manually this can be done by clicking on "Add Shipping" button from sale order create view. but How can I do this using ORM? 

I know that shipping method object is an instance of delivery.carrier model and I already know sale order record has a carrier_id field. So I tried this (here I'm creating a new sale order object and I want to assign a shipping method to it):
carrier = self.env['delivery.carrier'].search([(...)], limit=1)

order = self.env['sale.order'].create({..., 'carrier_id' : carrier.id,...})

but it doesn't work. how can I assign a shipping method to a sale order record using ORMs?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello,

To solve this you need to create your sales order and then call the "set_delivery_line" method on this object. I have written the code as a scheduled action to find a specific delivery method and find a specific sales order (you need to change this to create). The code will then call the set_delivery_line on the sales order object with the delivery method and price of the delivery method.

Code:

# search for the delivery method in products
delivery_method = env["delivery.carrier"].search([("name", "=", "Free delivery charges")])

# get the created sales oder
sale_order = env["sale.order"].search([("name", "=", "S00003")])

# create the sales order line for the delivery method
sale_order.set_delivery_line(delivery_method, delivery_method.fixed_price)


I hope this helps. 

Thanks, 

Ảnh đại diện
Huỷ bỏ

You are a god among men!!!

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 4 24
4015
2
thg 9 24
1476
1
thg 10 21
4618
1
thg 8 21
2166
0
thg 12 19
2472