Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.0][IMP]storage_image_product: main image of the variant #115

Merged
merged 1 commit into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions storage_image_product/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions storage_image_product/tests/test_product_image_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 attributes
"""
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 with the lowest sequence 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)