Jump to content

getProductCategories() [SOLVED]


Recommended Posts

I believe that this is a new function in Prestashop 1.5, there is very little documentation on it, and I am bashing my way through Prestashop to get things to work.

 

This code exits is the product class (product.php)

 

/**
* getProductCategories return an array of categories which this product belongs to
*
* @return array of categories
*/
public static function getProductCategories($id_product = '')
{
$ret = array();
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT `id_category` FROM `'._DB_PREFIX_.'category_product`
WHERE `id_product` = '.(int)$id_product
);if ($row)
foreach ($row as $val)
$ret[] = $val['id_category'];
return $ret;
}

 

How can I call this from my product.tpl template? I know that I'll end up with an array, and don't know how to call it or how to blow apart the array to get the values once I have called it.

 

I think that from the product.tpl template I would use this:

 

$product->getProductCategories($product->id_product);

 

But that is as far as I can get.

 

Thanks for any help. I know that I'm not the only person who will find any help with this really useful.

 

Thanks.

Edited by threeopus3 (see edit history)
  • Like 1
Link to comment
Share on other sites

Beauty. Thanks.

 

I changed that code a bit to match the format that I see being used on the ProductController.php.

 

I inserted this:

 

'categories' => $this->product->getProductCategories($this->product->id),

 

On my product.tpl page, I have added this code:

 

 {foreach from=$categories item=category}


  <p>I am a category</p>

 {/foreach}

 

 

I know it is rudimentary, but I can see that it is sort of working. I have a product that is associated with four categories, and I see that <p>I am a category</p> is echoed four times. This is good. Getting closer.

 

But, I can't figure out how to access that array of categories. I've tried:

 

 {foreach from=$categories item=category}
  {if $category.name|escape:'htmlall':'UTF-8' == 'On Sale'}

   <p>ON SALE</p>

  {/if}

 {/foreach}

 

This doesn't work, Though that syntax does work with the features array, so I thought it would. Do you have any idea how I can access the categories array information? Thanks again.

Edited by threeopus3 (see edit history)
Link to comment
Share on other sites

If I try printing $category.name, I get a list of single digit numbers (5,5,6,6,1). I don't know what they are. They are not category ids.

The array in my controller file is called $my_categories.

 

It seems like there is an array that already exists, called $categories. If I print it, I can get a list of the ids for every category in my store by calling "$category.id_category".

 

But I can't call "$category.id_category" for my product-specific array. I think I need to sleep on this and try again tomorrow.

Link to comment
Share on other sites

I've checked and it works fine in this case.

 

In ProductController.php, I add

 

'belonged_categories' => $this->product->getProductCategoriesFull($this->product->id)

 

Then in product.tpl, I add

 

{foreach from=$belonged_categories item=category}
Category ID#: {$category.id_category}<br />
Name: {$category.name}<br />
Friendly URL: {$category.link_rewrite}<br />
{/foreach}

 

You need to assign special variable name in ProductController.php, due to 'categories' is so common.

  • Like 1
Link to comment
Share on other sites

Prestamonster, thanks you so much!!! That works. It must have been, as you say, that I was using a variable that was too common.

 

Thank again! So this is how to add category info to the product page. I will call this solved.

Link to comment
Share on other sites

  • 5 weeks later...

Hello, But I try adding both codes but it instantly crashes ...

Could you give me more detail on how exactly get all that working. wich files and where exactly ?

 

thank you very much

 

 

I've checked and it works fine in this case.

 

In ProductController.php, I add

 

'belonged_categories' => $this->product->getProductCategoriesFull($this->product->id)

 

Then in product.tpl, I add

 

{foreach from=$belonged_categories item=category}
Category ID#: {$category.id_category}<br />
Name: {$category.name}<br />
Friendly URL: {$category.link_rewrite}<br />
{/foreach}

 

You need to assign special variable name in ProductController.php, due to 'categories' is so common.

Link to comment
Share on other sites

Hello, But I try adding both codes but it instantly crashes ...

Could you give me more detail on how exactly get all that working. wich files and where exactly ?

 

thank you very much

 

you want to display ALL product categories on product page? Am i right?

  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...