Jump to content

Display parent category's parent category-ID?


Recommended Posts

Hi,

 

Right now I can display the parent category ID simply by writing

$id_category_parent

 

But how would I go about displaying the parent category's parent category ID?

 

Example:

category 1

-> category 2

-> -> category 3

 

When I am on category 3, I would like to output category 3's parent category's parent category ID, which in the above example is 1.

 

 

Thanks in advance.

Link to comment
Share on other sites

You could probably simply use the function to get all parents of a category:

Category::getParentsCategories()

from file /classes/Category.php
Short description from file:

/**
* Get Each parent category of this category until the root category
*
* @param integer $id_lang Language ID
* @return array Corresponding categories
*/
public function getParentsCategories($id_lang = null)

See if that helps,
pascal

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 5 months later...

Hi Imran,

do you mean how to display all parents of the current category, up to the Home category on your page? Actually, that is done in the "Breadcrumb" already:

 

 

 

But you can use the function mentioned above if you want to do something else with the parents.

You will get an array in return, where you get all category-id's of the parents of the current category, up to the root.

So for example, if you are in SubCat3:

root

-Home

--SubCat1

---SubCat2

----SubCat3

 

If you process the function mentioned in my post above in your php file:

$parentCategories = $currentCategoryVariable->getParentsCategories();    (replace "$currentCategoryVariable" with the variable that holds the current category in your own function)

 

In our example, you will get an array with the following contents:

$parentCategories[0] = id_of_SubCat2

$parentCategories[1] = id_of_SubCat1

$parentCategories[2] = id_of_Home

$parentCategories[3] = id_of_Root

 
 

Maybe you should add not only the ID's to the array and give that to the TPL file, but first build an array of the full Parent Category info in the php file, and then make that available in the tpl file

 

So then add something like this to your php file:

 

foreach ($parentCategories as $parent)
{
  $parents[] = new Category($parent);
}
 

Then, to  make the array with the values available in the TPL file you can add something to you php file like this: 

 
 
$this->smarty->assign(array(
'parents' => $parents
));
 
 
(N.B. Maybe look at a sample in PrestaShop. For example, look in /modules/blockManufacturers/blockManufacturers.php):
 
$this->smarty->assign(array(
'manufacturers' => Manufacturer::getManufacturers(),
'text_list' => Configuration::get('MANUFACTURER_DISPLAY_TEXT'),
'text_list_nb' => Configuration::get('MANUFACTURER_DISPLAY_TEXT_NB'),
'form_list' => Configuration::get('MANUFACTURER_DISPLAY_FORM'),
'display_link_manufacturer' => Configuration::get('PS_DISPLAY_SUPPLIERS'),
));
 
You can then use the variable like this:
{foreach from=$manufacturers item=manufacturer name=manufacturer_list}
....
  <p>$manufacturer.name>/p>
...
{/foreach}
 
So, in our case,
you can then walk through our parents in the TPL file, using the smarty foreach function: 

 

{foreach from=$parents item=parent name=parents}
  {if !$smarty.foreach.parents.last}        {* if you don't want to go all the way to the root, but want to stop at Home, add this *}
 
     {* do something with the current parent, like show the name or so *}
     <p>$parent.name</p>
  {/if}
{/foreach}
 
 
 

Hope this helps. Just aks for more help, if you get stuck. Then please describe a little what you exactly want to do with the category parents.

 

pascal.

post-455771-0-20721200-1384354976_thumb.jpg

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