Search code examples
pythonodoo

How do I get the price of a product for a certain partner in Odoo?


It looks like the logic in Odoo for retrieving a product's unit price on a sale order is rather complex. I'm trying to find the simplest way to get the unit price without having to copy-paste source code that is likely to change between major Odoo versions.

The best I've come up with is to create a sale order and then unlink it immediately after so that it doesn't ever get committed to the database. That way I don't have to come up with any of the logic for getting the price of the product and I can just re-use the existing logic in the sale order model, which seems very future-proof. The issue with this is that the sale order ID is still incremented permanently even though the actual sale order record is never created in PostgreSQL. Here is the code:

partner = env["res.partner"].search([...], limit=1)
product = env["product.product"].search([...], limit=1)
sale_order = env["sale.order"].create({
    "partner_id": partner.id,
    "order_line": [(0, 0, {"product_id": product.id})]})

product_price = sale_order.order_line.read(["price_unit"])
sale_order.unlink()

Running it the first time, product_price is the following:

[{'id': 115566, 'price_unit': 44.0}]

Running it again, now product_price is this:

[{'id': 115567, 'price_unit': 44.0}]

I don't want to continue incrementing the ID for the records I'm creating when they're not even getting committed to the database. Is there a way I can get the product price easily without having to reinvent the wheel or creating phantom records?

EDIT:

Just want to clarify something. The reason I can't just pull the price right off the product model directly is because I want to take into account all the logic that normally occurs on a sale order, such as the partner's pricelist and the possibility of tax included prices. Odoo handles all this perfectly and so I want to re-use the existing logic without rewriting methods from the order line model.


Solution

  • After some more poking through Odoo, I found that there is a method that I can call like so:

    pricelist = partner.property_product_pricelist
    product._get_combination_info_variant(pricelist=pricelist)
    

    It seems to do what I want, but I still don't really know if this is the method I'm supposed to use to accomplish this.