Target specific languages with Prestashop attachments

nashville prestashop developer
JFIF;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 90 C     C   <<" }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ?vi^?idnX-19 СTW27_F? T{9nkHTp@'yߎ7T^YǏ- ),ux>QWy6)$c9? QxoxJ]7P'ؑ¿Hf$-W|n__uԵ BUmR_"1;P{1OZtMIɫ-[Z֑\Du_K| 'ei7~%^?{L +7;\jQG$e<[+]{E4RT5tΒATRF;Wi'ȉ7ȟ-¾6wIԴѵ[g/UsUCn]9< 0dvdx'csé,e# zt/o{Jՠ[HCϖ x8/z3ᗍ.u[YnoK-{99ڽsKImcp%yHPI`3b mN RRS%^UiA'%߆<]\C;5m &Z6F-wW] 0=i̠D*3@$ǐ~(a˵]ڝ/"cdہW M:vqZrEPQq`ǒ+$KZ-4)<++L7GVEQd8S%)xTklq0BQ@_M.+kpAmNŏ?@$$ڼBWSiцӖB ½o:$v6uiĪЂ rOrzy5Sxz]Iʜv φ͕,f?Wӟc^]7AOh|b!gu!9RW- i4ņ{)XAFNG(S`q֥Q*g]Tqm#,lcB Fއwh.VSp-T{q􌑻-TzdN:}+۴~EG=c'p}| %4 8cviݜH};W>i2̅%3@fEe?9wpGKTӒ̪e XrNdI71PАd{9Ht2$Y@Oxr[1A8h0~ ]e_ŬZ6-j:4oRA٭;%Þar:dJTsVNG I Warning: fopen( failed to open stream: HTTP wrapper does not support writeable connections in /home/dh42/public_html/blog/wp-includes/class-wp-image-editor.php on line 396

Attachments 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 Prestashop Attachments language-specific.

  • Version compatibility: Prestashop 1.4, Prestashop 1.5


So, you have a multilanguage store. And you also offer detailed spreadsheets of your products, or bonus videos, as downloadable attachments in product pages. There is a problem though: Prestashop doesn’t come with a way of targeting specific languages when adding attachments (even though the attachment name is translatable). How to cope with it, in case your attachments are really language-specific? With a little trick, we will take advantage of the default variables assigned to a product page and only display specific downloadable files.

Step 1 – Adding attachments

The first, and most important step is, of course, to add attachments to your products. I will use a demo product, the iPod Nano that comes out of the box with Prestashop. I will also make use of Prestashop 1.5 for this, even though the process can be applied in the same way with 1.4.

Fire up your admin panel and go to Catalog > Attachments (last link). Click on Add new in the top right corner

Here is the little secret: we will append the iso code for our language to the name of the attachment, so that it can be then targeted in the template. Let’s call our file demo_en. Don’t worry for the bad look it has, we will remove the useless part when displaying links. Add a description if you want (it’s not mandatory), and choose a file to upload. When you’re done, click Save. Note that the actual name of the file doesn’t matter at all, the important part is the attachment name. If you don’t know the iso code for your language, head over to Localization > Languages, and check the column highlighted in the following image:

Prestashop Languages Tab

Prestashop Languages Tab



Step 2 – Attaching our file to the product

Ok, we have our file, it’s time to add it to one of our products. Head over to a product page in the back office (I chose iPod Nano), and click on the Attachments tab in the left column. Here is what it’ll look like:

Prestashop Product Attachments

Prestashop Product Attachments

See the highlighted area? It displays attachments which are currently added to a product. It’s empty in my case, and I can see the attachment I just created is available in the left hand box. Select it, and click Add >> just below the box. Be sure you save the product now, otherwise the attachment won’t be added. If you successfully did this, after the refresh you should see the attachment appearing in the right hand box.

Note: You will see that you can also add attachments from this page. if you do, they’ll automatically be linked to this product, without having to add them from the left box.


Step 3 – Adding the language-specific logic

Now that the attachment has been sticked to our product, let’s head for the front office and check it out. Here it is:

Prestashop Attachments in product page

Prestashop Attachments in product page


Of course, it will be displayed for every language at this stage. Locate your theme folder, and open up product.tpl in your favorite code editor. We will be editing the attachments portion of this file to only display our languaged file.

Locate the following code (Prestashop 1.5.3 – at about line 518):

		{if isset($attachments) && $attachments}

If you’re on Prestashop 1.4, it will be more like this (default template)

		{if $attachments}

You will have the following block inside the condition:


Let’s start amending it to add some language logic, add this just after the div’s opening:

	{$lang_checker = {"_"|cat:{$lang_iso}}}
	{foreach from=$languages item=language} 
		{$lang_checker_array[] = {"_"|cat:{$language.iso_code}}}

Explanation: We are taking advantage of 2 variables here. First, we create a $lang_checker variable, consisting of an underscore plus the iso code of the current language of the store (this will help us target it in the attachment name); then, we create an array with all the languages’ iso codes, and as before, they’re prepended of an underscore. You can actually use any special character here, but you’ll of course need to be consitent with attachment names if you want to do it.

Now, amend the foreach statement as follows:

{foreach from=$attachments item=attachment}
	{if $|strstr:{$lang_checker}}

Refresh the page. You won’t see changes if your current language is the same as the attachment’s one, otherwise, you’ll notice the attachment is gone! Yep, that’s it, we are only displaying the attchment if it contains the actual iso code. We are also replacing (|replace:{$lang_checker}:”) the iso so that it’s removed from the name. Sweet.

Note: on Prestashop 1.4, the getPageLink must refer to attachment.php, not only attachment.

And we could stop here, careless of future implementation. But we are smart. The code needs to be more robust in case we change our mind and want to display some attachments to all the audience. At the moment, those ones would not be displayed due to our language restriction.


Step 4 – Fallback for non-languaged downloads

Add another attachment to this product, but this time don’t append any iso code. If you refresh the product page, you won’t be able to see the new attachment, of course. Let’s amend our previous code the following way:

{if isset($attachments) && $attachments}

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

{/if} {/if} {/foreach}


Explanation: A bit messy, but don’t be afraid. We added an else condition in case the iso code pattern is not found in the name. Inside it, we first setup a variable to check whether the file name contains any of the store languages’ iso codes. Remember we created an array with all the store languages before? it’s time to use it; the following iterates through it, and, if one of the languages matches, it sets $denier to true.

	{$denier = false}
	{foreach from=$lang_checker_array item=checkr_lang}
		{if $|strstr:{$checkr_lang}} 
			{$denier = true}

So basically we’re tellng the Prestashop: “If the file is setup to target a specific language, set $denier to true.”. The next line shows how to make proficient use of our $denier variable:

		{if !$denier}

If $denier is not set (read: if this file is not configured to target a specific language) add it to the list. Save&Refresh.

The non-language file is now added to the list!



And that’s it, you can now play around with different iso codes and see they will always show up the correct way. Of course, there are better way to implement such feature, like editing core files to make it seamless with Prestashop. Nonetheless, this is a cleaver way to achieve the result without heavy core modifications.

Prestashop expert developer dh42
by . Currently working as a Freelance Web Developer and Designer, Fabio is also the owner of the Prestashop Tutorials website Nemo’s Post Scriptum, to help anybody who wants to start an e-commerce website on this fantastic platform. As all developers, he hates Internet Explorer!