Prestashop 1.7: check if the product belongs to a specific category

Customize the product page and the category page.

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:

  1. Choose the category to be checked and get its ID directly from the Administration Panel > Catalog > Categories
  2. Choose where to display the new item or the item to be changed.
  3. 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/
  4. Use a text editor (for example: Notepad++) and open the chosen file.
  5. Scroll to the point where you want to make the change.
  6. Enter the following code:
    {if $product->id_category_default == ID of category}
      Here the code of the item to be displayed
    {/if}
  7. Replace “ID of category” with the numeric value of the chosen category.
  8. Save the file.
  9. If the change was made on a file downloaded on your computer, use your FTP access and overwrite the file in the correct position.
  10. 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)

  1. Choose the category to be checked and get its ID directly from the Administration Panel > Catalog > Categories
  2. Choose where to display the new item or the item to be changed.
  3. Use a text editor (for example: Notepad++) and open the file called product.tpl inside the folder
    /themes/nome-del-tema/templates/catalog/_partials/miniatures/
    or, if it does not exist, in the default theme folder
    /themes/classic/templates/catalog/_partials/miniatures/
  4. Scroll to the point where you want to make the change.
  5. Enter the following code:
    {if in_array(ID della categoria,Product::getProductCategories($product->id|intval))}
      Here the code of the item to be displayed
    {/if}
  6. Replace “ID of category” with the numeric value of the chosen category.
  7. Save the file.
  8. If the change was made on a file downloaded on your computer, use your FTP access and overwrite the file in the correct position.
  9. 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)

We are curious to read your opinion!

Leave a Reply

Your email address will not be published. Required fields are marked *

Respect for your privacy is our priority

Our website uses technical cookies to run properly and third-party cookies to generate reports on the use of navigation (statistical cookies). Show the list of cookies and the purpose of use
We can use technical cookies by law, but you have the right to choose whether or not to enable statistical cookies.

By enabling these cookies, you help us to provide you with a better experience.