From fd83d311889ad68b0ddcf35727fca8647d4ce670 Mon Sep 17 00:00:00 2001 From: snt Date: Thu, 12 Feb 2026 18:52:56 +0100 Subject: [PATCH] [FIX] product_sale_price_from_pricelist: Properly handle template vs variant IDs Instead of converting templates to variants before calling super(), check the model type when processing results. If working with product.template, get the variant from the template using browse(). This preserves the expected ID mapping in the result dictionary and avoids lambda variable binding issues. Fixes: KeyError: 9 in pricelist computation --- .../models/product_pricelist.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/product_sale_price_from_pricelist/models/product_pricelist.py b/product_sale_price_from_pricelist/models/product_pricelist.py index 4babf1f..14c2ba2 100644 --- a/product_sale_price_from_pricelist/models/product_pricelist.py +++ b/product_sale_price_from_pricelist/models/product_pricelist.py @@ -16,15 +16,6 @@ class ProductPricelist(models.Model): ProductPricelistItem = self.env["product.pricelist.item"] ProductProduct = self.env["product.product"] - # Ensure we're working with product.product, not product.template - if products and products._name == "product.template": - # Convert templates to their variants - _logger.info( - "[PRICELIST DEBUG] Converting product.template to product.product: %s", - products.ids, - ) - products = products.mapped("product_variant_ids") - _logger.info( "[PRICELIST DEBUG] _compute_price_rule called with products=%s (model=%s), quantity=%s", products.ids, @@ -55,7 +46,22 @@ class ProductPricelist(models.Model): item_id, ) if item.base == "last_purchase_price": - product = ProductProduct.browse(product_id) + # product_id could be from product.template or product.product + # Check which model we're working with + if products._name == "product.template": + # Get the variant from the template + template = products.browse(product_id) + if template.exists(): + product = template.product_variant_id + else: + _logger.warning( + "[PRICELIST] Template ID %s not found in products", + product_id, + ) + continue + else: + product = ProductProduct.browse(product_id) + price = product.last_purchase_price_received _logger.info( "[PRICELIST DEBUG] Product %s: last_purchase_price_received=%s, item.price_discount=%s",