EMail Templates: How to include thumbnails?

Hello!

The email templates, esp. order confirmation and updates, should contain thumbnails of the lineitems. The mediaId is saved in lineItem.coverId.

 

I have tried to solve it with the  **searchMedia() **function           

{# search for media in database #}
{% set media = searchMedia([lineItem.coverId], context.context) %}

{# extract mediaentity object #}
{% set mediaEntity = media.get(lineItem.coverId) %}

 {# get url #}
 {% set mediaUrl = mediaEntity.getUrl() %}
{{mediaUrl}}

=> official docu: https://docs.shopware.com/en/shopware-platform-dev-en/how-to/custom-fields-media-type
=> discussion: https://forum.shopware.com/discussion/65543/customfields-media-ausgabe

The problem seems to be that the context isn’t existing in the mail templates. Also, there is an empty output when I am using {{ dump() }} ; although the environment is set to DEV mode

Do you have any ideas how to solve this?
How do you include the images? Or how do you get the image url?

Thanks!

1 „Gefällt mir“

Any new regarding this issue?

As mentioned in another issue: Bilder in Email-Bestätigungen - #12 von FloC3
This worked for me

<img src="{{ searchMedia([lineItem.coverId], context).first()|sw_encode_media_url }}" width="100" />
3 „Gefällt mir“

This is great - now we have the URL! :slight_smile: But is there a possibility to take the pre-generated thumbnails (e.g. 100px), like with sw_thumbnails?

{% set id = lineItem.coverId %}
{% set mediaCollection = searchMedia([id], context) %}
{% set mediaImage = mediaCollection.get(id) %}

{% for item in mediaImage.thumbnails.elements %}
{% if item.width == 190 %}
  <img src="{{ item.url }}">
{% endif %}
{% endfor %}

I dumped it inside my base.html.twig. To test and look around. I got it to work there with sw_thumbnails. But got an [object Promise] error. So I looked further in the information my dump gave me and came up with this solution. You can replace the 190 with any other thumbnail resoulution your shop gives you.