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

Customize the product page and the category page.

  1. Home
  2. /
  3. Prestashop Tutorials
  4. /
  5. Prestashop 1.7: check if the product belongs to a specific category

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/name of theme/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 of category,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!

10 thoughts on "Prestashop 1.7: check if the product belongs to a specific category"

  1. A Amos says:

    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?

  2. Roberto Vitolo says:

    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

  3. Matt says:

    Hi
    Posiible on order-confirmation page ?

    1. Roberto Vitolo says:

      Hi Matt.

      In Order Confirmation page, you could use {$product.id_category_default} inside order-confirmation-table.tpl (/templates/checkout/_partials/)

      Best Regards

  4. Sandrine Bud says:

    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

    1. Roberto Vitolo says:

      Hi Sandrine.

      In product page as well as in category, you could use something similar: {$breadcrumb.links[1].title}

      Best Regards

  5. Justyna says:

    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.

    1. Roberto Vitolo says:

      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

  6. edgar says:

    How do I know if I’m page of products or categories?

    1. Roberto Vitolo says:

      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

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.