Jump to content

Check if product is in specific category?


mbp

Recommended Posts

Hi,

 

I'm trying to check if a product is located in a specific category and change the label of add to cart button in product-list.tpl

 

In this case i would like to display "Read more here" instead of "Add to cart" if product is located in category id 14.

 

Right now all products from category 14 is getting "Add to cart"

 

I'm running 1.6.0.11

{assign var='associated' value=0}                
  {foreach Product::getProductCategories($smarty.get.id_product) as $category}
    {if in_array($category, 14)}
      {assign var='associated' value=1}
    {/if}
  {/foreach}
{if $associated==1}
  {l s='Read more here'}
{else}
  {l s='Add to cart'}
{/if}

The code was written by veika in this tread: https://www.prestashop.com/forums/topic/324970-solved-if-product-is-in-category-a-or-in-category-b-or-in-category-c-then-do-this/

Link to comment
Share on other sites

There are a few small errors/over complications in the code: 

- The in_array's parameters should be reversed, The first parameter is  the 'needle' that you are looking for (i.e. the specific category like your 14), the 2nd parameter is the 'haystack' (i.e. the array of ID's associated with the product) 

 

- the foreach loop is not necessary, as the return value of the getProductCategories function is just a single array of ID's, so you can directly search through it with in_array.

- Because the foreach loop is not necessary anymore, the 'associated' variable is also unnecessary. We can just directly add the texts, depending on the result of the in_array comparison

 

- We don't have to get the id_product through querying the $smarty variable, as it is already available through our smarty variable '$product' (i.e $product->id)

 

So replace the above code with:

 

{if in_array(14,Product::getProductCategories($product->id|intval))}
  {l s='Read more here'}
{else}
  {l s='Add to cart'}
{/if}
 
 
Hope that does the trick :-)
 
pascal.
  • Thanks 1
Link to comment
Share on other sites

  • 6 months later...

 

There are a few small errors/over complications in the code: 

- The in_array's parameters should be reversed, The first parameter is  the 'needle' that you are looking for (i.e. the specific category like your 14), the 2nd parameter is the 'haystack' (i.e. the array of ID's associated with the product) 

 

- the foreach loop is not necessary, as the return value of the getProductCategories function is just a single array of ID's, so you can directly search through it with in_array.

- Because the foreach loop is not necessary anymore, the 'associated' variable is also unnecessary. We can just directly add the texts, depending on the result of the in_array comparison

 

- We don't have to get the id_product through querying the $smarty variable, as it is already available through our smarty variable '$product' (i.e $product->id)

 

So replace the above code with:

 

{if in_array(14,Product::getProductCategories($product->id|intval))}
  {l s='Read more here'}
{else}
  {l s='Add to cart'}
{/if}
 
 
Hope that does the trick :-)
 
pascal.

 

 

Pascal, How can I pass multiple categories ID's?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...