Jump to content

Get category parent in product.tpl


Recommended Posts

I'm making a site that sells comics, with the following category tree structure:

 

- PARENT CATEGORY

-- SUBCATEGORY

-- SUBCATEGORY

-- SUBCATEGORY

 

Like this:

 

- Publisher

-- Marvel

-- DC

-- Dark Horse

 

- Genre

-- Horror

-- Crime

-- Comedy

 

- Writer

-- Grant Morrison

-- Joss Whedon

-- Mark Millar

 

Every book is tied to several subcategories. For example:

Publisher: Dark Horse

Genre: Horror

Writer: Mark Millar

 

I have the following code that gives me a list of the subcategories that every book is in:

{foreach from=Product::getProductCategoriesFull(Tools::getValue('id_product')) item=cat}

<li>{$cat.name}</li>

{/foreach}

It returns something like this:

<li>Marvel</li>
<li>Horror</li>
<li>Mark Millar</li>

But I want to be able to show the name of the parent category before each subcategory.

<li>Publisher: Marvel</li>
<li>Genre: Horror</li>
<li>Writer: Mark Millar</li>

How can I do that? Is there a way to get the id or the name of the parent category inside a foreach in product.tpl?

Edited by nico.r (see edit history)
Link to comment
Share on other sites

Each category knows it's parent:

 

if you have a category $category

 

you can reach its parent id like this

$category->id_parent

Then you can use the id to get the full info on the category

$parent = new Category($category->id_parent)

 

Something like this,

 

Hope it helps,

pascal.

Link to comment
Share on other sites

  • 2 weeks later...

Found the soultion :)

First of all you should use getProductCategoriesParent instead getProductCategoriesFull. The thing is this function, placed in /classes/product.php on line 2343 (presta 1.5.6), doesn't work because of one small mistake. It should be changed to this:

public static function getProductCategoriesParent($id_product = '', $id_parent = '', $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 c.`id_parent` = '.(int)$id_parent.' AND cl.`id_lang` = '.(int)$id_lang //<-- bug was in this line - originally there's cl.'id_parent'
		);

		foreach ($row as $val)
			$ret[$val['id_category']] = $val;

		return $ret;
	}

Originally there's cl.'id_parent' which means 'id_parent' is taken from wrong table ('cl' is associated with ps_category_lang) that doesn't even have column named id_parent. It's in ps_category table so you just have to change cl.'id_parent' to c.'id_parent'. Now your good to go with .tpl changes. Put this wherever you want product categories to be displayed:

<div class="productcats">
    <p>{l s='Designer'}:
        {foreach from=Product::getProductCategoriesParent(Tools::getValue('id_product'), 7) item=cat}
             <a href="{$link->getCategoryLink({$cat.id_category})}" title="{$cat.name}">{$cat.name}</a>
        {/foreach}
    </p>
    <p>{l s='Places'}:
        {foreach from=Product::getProductCategoriesParent(Tools::getValue('id_product'), 88) item=cat}
             <a href="{$link->getCategoryLink({$cat.id_category})}" title="{$cat.name}">{$cat.name}</a>
        {/foreach}
    </p>
    <p>{l s='Furniture'}:
        {foreach from=Product::getProductCategoriesParent(Tools::getValue('id_product'), 56) item=cat}
             <a href="{$link->getCategoryLink({$cat.id_category})}" title="{$cat.name}">{$cat.name}</a>
        {/foreach}
    </p>
</div>

Notice that there are specific id_parent given in each section which represents parent category's id number, in my case 7 for Designer, 88 for Places and 56 for Furniture. It's because I just need 3 of 9 main categories to be displayed this way. Otherwise you may use another foreach loop for getting all main categories.

 

Enjoy! :D

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

  • 1 year later...

Hey Sharak! This is a great solution! Works fine with 1.6, too after readding the (removed?!) function "getProductCategoriesParent"  in product.php again.

 

But do you think it is possible to show a specific Subcategory greater then the nearest parent? means following:

 

Start

|->Category1

|-|->Subcategory1.1

|-|-|->Subcategory1.2.1

|-|-|->Subcategory1.2.2

|-|-|->Subcategory1.2.3

|->Category2

|-|->Subcategory2.1

|-|-|->Subcategory2.2.1

|-|-|->Subcategory2.2.2

 

And on the product detail page the following:

 

"This Product belongs to:

 

Category1:

 -Subcategory1.2.2

 -Subcategory1.2.3

 

Category2:

-Subcategory2.2.2

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

×
×
  • Create New...