Skip to Content
Menu
This question has been flagged
1 Reply
3108 Views

Quiero sumar las cantidades de los productos por id o nombre, es decir si tengo 3 productos iguales en un campo one2many, cada uno con diferente cantidad y luego se agrega 2 veces otro producto con diferente cantidad, al final Quiero mostrar la cantidad total en otro campo que muestre el nombre y la cantidad total de los dos productos

Avatar
Discard
Best Answer

Hello,

First you need to define a function according to your requirement, In the function you need to get the orderlines of corresponding _order,

ie,

order_lines = self.env['sale.order.line'].search([('order_id', '=', self.id)])

now you need to get the products and corresponding quantity, for that

product = []
qty = []
for order in order_lines:
if order.product_id not in product:
product.append(order.product_id)
qty.append(order.product_uom_qty)
else:
index = product.index(order.product_id)
qty[index] += order.product_uom_qty

Here you will get each product and their qty respectively in the lists product and qty. You can add the values from the list to the needed field by looping them.

Regards

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 23
4101
3
Jul 24
6759
1
Aug 23
2558
1
Dec 22
3805
0
Oct 22
2945