Sometimes it may be necessary to modify some element of the product page, or add new ones, in case a product belongs to a specific category.
Product Page
The procedure to perform is really simple:
- Choose the category to be checked and get its ID directly from the Administration Panel > Catalog > Categories
- Choose where to display the new item or the item to be changed.
- Then discover the .tpl file inside the folder
/themes/name-of-theme/templates/catalog/_partials/
or, if it does not exist, in the default theme folder/themes/classic/templates/catalog/_partials/
- Use a text editor (for example: Notepad++) and open the chosen file.
- Scroll to the point where you want to make the change.
- Enter the following code:
{if $product->id_category_default == ID of category} Here the code of the item to be displayed
{/if} - Replace “ID of category” with the numeric value of the chosen category.
- Save the file.
- If the change was made on a file downloaded on your computer, use your FTP access and overwrite the file in the correct position.
- It could be necessary to delete the cache of Prestashop (Advanced Parameters > Performance) and of your browser. (Please, be sure that “Recompile templates if the files have been updated” is enabled)
Category (product list)
- Choose the category to be checked and get its ID directly from the Administration Panel > Catalog > Categories
- Choose where to display the new item or the item to be changed.
- Use a text editor (for example: Notepad++) and open the file called product.tpl inside the folder
/themes/name of theme/templates/catalog/_partials/miniatures/
or, if it does not exist, in the default theme folder/themes/classic/templates/catalog/_partials/miniatures/
- Scroll to the point where you want to make the change.
- Enter the following code:
{if in_array(ID of category,Product::getProductCategories($product->id|intval))} Here the code of the item to be displayed
{/if} - Replace “ID of category” with the numeric value of the chosen category.
- Save the file.
- If the change was made on a file downloaded on your computer, use your FTP access and overwrite the file in the correct position.
- It could be necessary to delete the cache of Prestashop (Advanced Parameters > Performance) and of your browser. (Please, be sure that “Recompile templates if the files have been updated” is enabled)
Thanks for your tutorial. I tried the Category (product list) example and get the following error when debug is switched on:
Warning: htmlspecialchars() expects parameter 1 to be string, array given
My code is:
{if in_array(26, Product::getProductCategories($product->id|intval))}
{/if}
Can you provide some advice on what I might have done wrong?
Hi.
Thanks for reading this article.
The error (warning) could be caused by other code on the page, by the theme used, a module or an override.
You should investigate the situation and see if the error message contains information on where the problem begins.
Best Regards
Hi
Posiible on order-confirmation page ?
Hi Matt.
In Order Confirmation page, you could use {$product.id_category_default} inside order-confirmation-table.tpl (/templates/checkout/_partials/)
Best Regards
Hello,
Thank you for your tutorial.
Your code works great, except I’m trying to target the root category of my product, not its specific category.
Is there anyway to do this using your code? Both for the category and product pages…
Thanks for your response 🙂
Sandrine
Hi Sandrine.
In product page as well as in category, you could use something similar: {$breadcrumb.links[1].title}
Best Regards
What to do if i want to display some block on product page, when product is in specific category (parent, but not default / main) ?
Then is not working:
{if $product->id_category_default == 7}
Wymiary
{hook h=”CETemplate” id=”4″}
{/if}
It working only withdefault / main category.
Hi Justyna.
Assuming you want to check if the product belongs to category with ID 7,
in product page you could use something like that:
{foreach from=Product::getProductCategoriesFull(Tools::getValue(‘id_product’)) item=cat}
{if $cat.id_category == 7}
your code here
{/if}
{/foreach}
Best Regards
How do I know if I’m page of products or categories?
Hi edgar.
If your ecommerce is based on Prestashop 1.7, you can use this piece of code inside a tpl file of your theme:
{if $page.page_name == ‘category’}
yes, it’s a category page
{elseif $page.page_name == ‘product’}
yes, it’s a product page
{/if}
You can read further at this page:
https://www.themebooster.com/prestashop-tutorials/prestashop-1-7-smarty-global-variables/ (Prestashop 1.7: examples of conditions)
Best Regards