Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
355 Lượt xem

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 ?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

Worked great, Thanks!

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 10 25
257
Tiered discounts Đã xử lý
1
thg 4 25
1622
1
thg 11 24
1885
1
thg 10 24
2196
2
thg 10 24
3513