Jump to content

Display category name by category ID


Recommended Posts

Hi everyone.

 

I have a question concerning categories - I'm getting their ID's on product or product list page using:

 

{$product->id_category_default}

 

Now when I have the ID, i would like to display different info about this category e.g. category name, description etc and I guess it should be quite simple function.

 

Another question is for features. There is loop in product page that displays all the features for the product:

 

{foreach from=$features item=feature}

<li><span class="feature_name">{$feature.name|escape:'htmlall':'UTF-8'}:</span>

{$feature.value|escape:'htmlall':'UTF-8'}</li>

{/foreach}

 

Is it possible to exclude some features from that list? Or include only few features?

Link to comment
Share on other sites

  • 4 months later...
  • 1 month later...
  • 3 months later...

Hi,

yes, you can display all sort of data for the default category of a product, since the category object is assigned in the product page.

i did a dump of the object for the ipod nano in a test store, here are the most useful properties 8you can access them like $category->name in the template):

 

public 'id' => int 2

public 'id_category' => string '2' (length=1)

public 'name' => string 'iPods' (length=5)

public 'active' => string '1' (length=1)

public 'position' => string '0' (length=1)

public 'description' => string 'Now that you can buy movies from the iTunes Store and sync them to your iPod, the whole world is your theater.' (length=110)

public 'id_parent' => string '1' (length=1)

public 'level_depth' => string '1' (length=1)

public 'nleft' => string '2' (length=1)

public 'nright' => string '11' (length=2)

public 'link_rewrite' => string 'music-ipods' (length=11)

public 'meta_title' => string '' (length=0)

public 'meta_keywords' => string '' (length=0)

public 'meta_description' => string '' (length=0)

public 'date_add' => string '0000-00-00 00:00:00' (length=19)

public 'date_upd' => string '2012-07-27 16:23:29' (length=19)

public 'groupBox' => null

 

For the features, i always put it ids or features names to exclude/inslue certain ones, like

 

{foreach from=$features item=feature}
{if feature.name == 'Coolness'}
<li><span class="feature_name">{$feature.name|escape:'htmlall':'UTF-8'}:</span>
{$feature.value|escape:'htmlall':'UTF-8'}</li>
{/if}
{/foreach}

 

 

Cheers!

Link to comment
Share on other sites

  • 1 year later...

Sorry for digging this topic up but it doesn't say "solved" and I have similar problem :P

 

I want to get specific category information by category_id on every page, not  only category or product related. No problem with link, I just use {$link->getCategoryLink('category_id')}. But how to get category name? Is there any global function for that?

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

  • 4 weeks later...

hello,

 

Go look this method in Product.php. 

 

public static function getProductCategoriesFull($id_product = '', $id_lang = null)

{
if (!$id_lang)
$id_lang = Context::getContext()->language->id;
 
$ret = array();
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT cp.`id_category`, cl.`name`, cl.`link_rewrite` FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.id_category = cp.id_category)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cp.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').')
'.Shop::addSqlAssociation('category', 'c').'
WHERE cp.`id_product` = '.(int)$id_product.'
AND cl.`id_lang` = '.(int)$id_lang
);
 
foreach ($row as $val)
$ret[$val['id_category']] = $val;
 
return $ret;
}
Link to comment
Share on other sites

  • 1 year later...

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...