Skip to Content
Menu
This question has been flagged
1 Reply
2546 Views


Hi,


It's easier to explain my question with this example.

I have the following products on a quotation:

PRODUCT
VARIANT
QUANTITY

PRICE

T-Shirt
Blue
1
$100
T-Shirt
Red
1
$100
Pants
Blue
1
$100
Pants
Black
1
$100
Hat
Black
1
$100


I'm trying to add a list of unique (distinct) products with their description and image to report_saleorder_document, like this:

T-Shirt

T-Shirt's sale description
T-Shirt's image
Pants

Pants' sale description
Pants' image
Hat

Hat's sale description
Hat's image


My code so far (thanks to @Niyas Raphy for his help)

<t t-foreach="set(doc.order_line.mapped('product_id'))" t-as="x">
<table border="0" width="100%">
<thead>
<tr>
<th style="background: #F0F0F0; text-align: left">
<p><t t-esc="x.name"/></p>
</th>
<th style="background: #F0F0F0; text-align: left">
</th>
</tr>
</thead>

<tbody>
<tr>
<td style="width: 70%; text-align: justify; vertical-align: top;">
<p><t t-esc="x.description_sale"/></p>
</td>
<td style="width: 70%; text-align: justify; vertical-align: top;">
<img t-attf-src="data:image/*;base64,{{x.product_id.image_1920}}"/>
</td>
</tr>
</tbody>
</table>
</t>


But this code doesn't give me unique (distinct) products, it repeats the products that have already been listed and the result is something like this

T-Shirt

T-Shirt's sale description + image

T-Shirt

T-Shirt's sale description + image

...


How can I change my code to get only unique products names/description/image?




Avatar
Discard
Best Answer

Hi
You can use t-set to get a list of product name and t-if after <thead> tag to check current product exist in the list.


<t t-set="product_names" t-value="''"/>
<t t-foreach="set(doc.order_line.mapped('product_id'))" t-as="x">
<table border="0" width="100%">
<t t-if="x.name not in products_names">
<thead>
<tr>
<th style="background: #F0F0F0; text-align: left">
<p><t t-esc="x.name"/></p>
<t t-set='product_names' t-value="product_names+','+x.name"/>
</th>
<th style="background: #F0F0F0; text-align: left">
</th>
</tr>
</thead>

<tbody>
<tr>
<td style="width: 70%; text-align: justify; vertical-align: top;">
<p><t t-esc="x.description_sale"/></p>
</td>
<td style="width: 70%; text-align: justify; vertical-align: top;">
<img t-attf-src="data:image/*;base64,{{x.product_id.image_1920}}"/>
</td>
</tr>
</tbody>
</t>
</table>
</t>

Hope this will help you

Thank You.


Avatar
Discard
Author

Works perfectly! Thank you!!!

For anyone who also have the same problem, the code just needs two adjustments:

Line 1 - Change "product_names" to "products_names"

LIne 22 - Change x.product_id.image_1920 to x.image_1920