Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
129 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
říj 25
156
1
dub 25
1560
1
lis 24
1794
1
říj 24
2083
2
říj 24
3412