diff --git a/storage_image_product/models/product_product.py b/storage_image_product/models/product_product.py index aec6cb30f3..bb8537f134 100644 --- a/storage_image_product/models/product_product.py +++ b/storage_image_product/models/product_product.py @@ -55,6 +55,16 @@ def _compute_main_image_id(self): record.main_image_id = record._get_main_image() def _get_main_image(self): + match_image = self.variant_image_ids.filtered( + lambda i: i.attribute_value_ids + == self.mapped( + "product_template_attribute_value_ids.product_attribute_value_id" + ) + ) + if match_image: + return fields.first( + match_image.sorted(key=lambda i: (i.sequence, i.image_id)) + ).image_id return fields.first( self.variant_image_ids.sorted(key=lambda i: (i.sequence, i.image_id)) ).image_id diff --git a/storage_image_product/tests/test_product_image_relation.py b/storage_image_product/tests/test_product_image_relation.py index 93ab798ff4..98acf2fdc1 100644 --- a/storage_image_product/tests/test_product_image_relation.py +++ b/storage_image_product/tests/test_product_image_relation.py @@ -114,3 +114,39 @@ def test_main_image_and_urls(self): (self.logo_image, self.product_b), ) self._test_main_images_and_urls(expected) + + def test_main_image_attribute(self): + """ + Attach the image to the template and check the first image of the + variant is the one with same attriubttes + """ + self.env["product.image.relation"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.logo_image.id, + "sequence": 1, + } + ) + self.env["product.image.relation"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.white_image.id, + "attribute_value_ids": [ + ( + 6, + 0, + [ + self.env.ref("product.product_attribute_value_4").id, + self.env.ref("product.product_attribute_value_1").id, + ], + ) + ], + "sequence": 10, + } + ) + # The variant should not take the only wiht lower sequencce but + # the one with same attributes + expected = ((self.white_image, self.product_b),) + self._test_main_images_and_urls(expected) + expected = ((self.logo_image, self.product_c + self.product_a),) + self._test_main_images_and_urls(expected)