[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
This commit is contained in:
snt 2026-02-12 18:52:56 +01:00
parent 4b78dc4447
commit fd83d31188

View file

@ -16,15 +16,6 @@ class ProductPricelist(models.Model):
ProductPricelistItem = self.env["product.pricelist.item"] ProductPricelistItem = self.env["product.pricelist.item"]
ProductProduct = self.env["product.product"] 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( _logger.info(
"[PRICELIST DEBUG] _compute_price_rule called with products=%s (model=%s), quantity=%s", "[PRICELIST DEBUG] _compute_price_rule called with products=%s (model=%s), quantity=%s",
products.ids, products.ids,
@ -55,7 +46,22 @@ class ProductPricelist(models.Model):
item_id, item_id,
) )
if item.base == "last_purchase_price": if item.base == "last_purchase_price":
# 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) product = ProductProduct.browse(product_id)
price = product.last_purchase_price_received price = product.last_purchase_price_received
_logger.info( _logger.info(
"[PRICELIST DEBUG] Product %s: last_purchase_price_received=%s, item.price_discount=%s", "[PRICELIST DEBUG] Product %s: last_purchase_price_received=%s, item.price_discount=%s",