Skip to Content
Menú
This question has been flagged
1 Respondre
125 Vistes

Follow-up on this post: https://www.odoo.com/nl_NL/forum/help-1/product-view-show-price-of-a-specific-pricelist-287616

I now have the following code:

for record in self:

    pricelist = self.env['product.pricelist'].search([('name', '=', 'Horeca')], limit=1)

    product_variant = record.product_variant_id

    record["x_studio_horeca_price"] = pricelist._get_product_price(product_variant, 1.0) if pricelist and product_variant else 0.0

This works great in the product view, but if I have 2 variants, only the base variant has the correct price. With the second variant, where I add a certain amount, the pricelist price is calculated on the original price, not the variant sale price. 

Any ideas on how to solve this? Should I create a separate field for the variants or can I adapt the existing code ?

Avatar
Descartar
Best Answer

Hi,


The issue arises because the current code calculates prices based on the product template’s base price, not the specific variant’s adjusted price. In Odoo, only the first variant works correctly since product_variant_id it defaults to one variant, and _get_product_price doesn’t automatically include variant extras unless it’s run on the variant itself. To fix this, the computation should be done on the product.product (variant) instead of the template. Ideally, the custom field should be defined on product.product, so each variant has its own Horeca price from the pricelist. If kept on the template, only one price will display, making it better to move the field to variants for accuracy.


Try the following code,


for record in self:

    pricelist = self.env['product.pricelist'].search([('name', '=', 'Horeca')], limit=1)


    product_variant = record if record._name == 'product.product' else record.product_variant_id


    if pricelist and product_variant:

        record["x_studio_horeca_price"] = pricelist._get_product_price(product_variant, 1.0)

    else:

        record["x_studio_horeca_price"] = 0.0


Hope it helps

Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
d’oct. 25
153
1
d’abr. 25
1559
1
de nov. 24
1783
1
d’oct. 24
2082
2
d’oct. 24
3410