Jump to content

usage of getParentsCategories()


lsantos2015

Recommended Posts

Hello..
 
I'm with a little problem, when trying to get the full category parent array.
 
I'm editing CategoryController.php, and need to get 
 

$var = CategoryCore::getParentsCategories($this->context->language->id);

 
working, and I keep getting this
 

Strict Standards: Non-static method CategoryCore::getParentsCategories() should not be called statically, assuming $this from incompatible context in /xxx/xxx/public_html/xxx/controllers/front/CategoryController.php on line 9999999

 
I'm how can I make this work, to retrieve an array with all categories id's..
 
Thanks in advance

Link to comment
Share on other sites

this function is not static:

 

classes/Category.tpl

/**
	 * 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)
	{
		$context = Context::getContext()->cloneContext();
		$context->shop = clone($context->shop);

		if (is_null($id_lang))

it means that you can't call it with something::function

you can call it only with

 

$category = new Category();

$category->getParentsCategories();

 

 

----------------

or just change it to static function

 

public static function getParentsCategories($id_lang = null)
{
$context = Context::getContext()->cloneContext();
$context->shop = clone($context->shop);

 

then you will be able to call it with Category::getParentCategories

Link to comment
Share on other sites

HI..

 

Thanks for the prompt answer.

 

If I change to static, I get this : 

Fatal error: Using $this when not in object context in /home3/lbss2015/public_html/rmc/classes/Category.php on line 1061

and using 

$category = new Category();
$category->getParentsCategories();

and using this, where do I put the actual category, because all I can get is a lot of empty fields, and the value 1, after 4 or 5 fields from the array..

 

This also does not work.

$category = new Category($this->category->id);
$category->getParentsCategories();

I'm a bit lost here..

Link to comment
Share on other sites

If you're adding something to the category controller itself, it should already have access to the category variable stored within itself, so could just try using:

$this->category->getParentsCategories();

If you don't have any luck with that, could also try:

$this->context->category->getParentsCategories();

That said, this bit you mentioned should have worked.. just to check, is it throwing an error, or are you just not storing the output of the function?

$category = new Category($this->category->id);
$category->getParentsCategories();

i.e. $var = $category->getParentsCategories();

Link to comment
Share on other sites

If object category has been initialized somewhere within your code, you don't have to initializing it again.

 

So, code below is inappropriate :

$category = new Category($this->category->id);
$array_parent = $category->getParentsCategories();

To get each parent category from the existing object category, you just have to do like this :

// Get array parent from the existing object Category
$array_parent = $this->category->getParentsCategories();

But if the object category doesn't yet initialized, then you should do something like this :

// Define id_category
$id_category = (int)Tools::getValue('id_category');

// Initialize object Category
$category = new Category((int)$id_category);

// Get array parent from the initialized object Category
$array_parent = $category->getParentsCategories();
  • Like 1
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...