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",