Creating Multilanguage attachments in PrestaShop 1.7

Attachments are a great way to provide your customers with rich pre-sale information about your products. But what if your site is multilingual? Let’s see how to make Attachments multilanguage in PrestaShop 1.7.

Watch the screencast

Text Version

Step 1 – Adding the attachments

Adding attachments with the proper name is fundamental for this technique. We will append the language ISOs to the names, so that they can later be targeted in the template.

Login to the back office, then navigate to Catalog > Products, and choose one to start with. At the bottom of the Options tab, add two test attachments. The file names don’t matter, but the title you choose does. Add one attachment for each language it’s translated to, making sure you append the ISO code for that language after the name, like this: _en, _it, _fr.

Note: If you don’t know what the language code is, hop to International > Localization, and click on Languages. The ISO code is listed in the table:

PrestaShop 1.7 Language ISO

 

If you need an attachment to display for all of the languages, make sure you do not add any language ISO to it.

Step 2 – Editing the template

 

For this tutorial I am using the “classic” template, that ships with PrestaShop 1.7. If you are using a custom theme, be aware that some of the code might be different, or even located in a different file.

Open up themes/classic/templates/catalog/product.tpl.

Locate the following code:

{if $product.attachments}
                    <div class="tab-pane fade in" id="attachments" role="tabpanel">
                       <section class="product-attachments">
                         <h3 class="h5 text-uppercase">{l s='Download' d='Shop.Theme.Actions'}</h3>

Right afterwards, but before the foreach loop, add the following

{$lang_checker = "_"|cat: {$language.iso_code}}
{$lang_checker_array = ['en', 'it']}

Make sure you use your own ISO codes instead of ‘en’ and ‘it’. Add all of the languages your store supports

Next, wrap the attachment block in the following IF statement

                         {if $attachment.name|strstr:{$lang_checker}}
                           <div class="attachment">
                             <h4><a href="{url entity='attachment' params=['id_attachment' => $attachment.id_attachment]}">{$attachment.name|replace:{$lang_checker}:''}</a></h4>
                             <p>{$attachment.description}</p
                             <a href="{url entity='attachment' params=['id_attachment' => $attachment.id_attachment]}">
                               {l s='Download' d='Shop.Theme.Actions'} ({$attachment.file_size_formatted})
                             </a>
                           </div>
{/if}

If you don’t need some attachments to display for all languages, you can go ahead and test it, as the basic part if basically done. If you do, you will need to add a little extra condition, and we will use that lang_checker_array for it.

Step 2.1 – Fallback for generic attachments

Right before the end of the IF statement that checks for our ISO code, add the following:


{else}
  {$denier = false}
  {foreach from=$lang_checker_array item=checkr_lang}
                              {if $attachment.name|strstr:{$checkr_lang}}
                              {$denier = true}
                              {/if}
  {/foreach}

Explanation: We are setting a variable (denier) that will prevent displaying the attachment if it contains any language ISO. Because of this logic, make sure you use a string that’s unique enough in case your attachment names contain product codes or alike, otherwise they won’t ever display.

Right afterwards, but still inside our main IF, add this last chunk of code

                               
{if !$denier}
<div class="attachment">
<h4><a href="{url entity='attachment' params=['id_attachment' => $attachment.id_attachment]}">{$attachment.name|replace:{$lang_checker}:''}</a></h4>
<p>{$attachment.description}</p
<a href="{url entity='attachment' params=['id_attachment' => $attachment.id_attachment]}">
{l s='Download' d='Shop.Theme.Actions'} ({$attachment.file_size_formatted})
</a>
</div>
{/if}

Which is basically a copy of the standard attachment code. If the attachment does not contain any iso code, it means we can display it normally.

We are done!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • https://www.spielezar.ch/ Emanuel Schiendorfer

    Nice idea :)

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: