跳至内容
菜单
此问题已终结
1 回复
279 查看

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 ?

形象
丢弃
最佳答案

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

形象
丢弃
编写者

Worked great, Thanks!

相关帖文 回复 查看 活动
1
10月 25
220
1
4月 25
1603
1
11月 24
1849
1
10月 24
2148
2
10月 24
3479