Page 1 of 1

In final Leaf Category, Show Same Level Categories

Posted: Tue Dec 08, 2009 5:33 am
by moggiex
Howdy,

Hoping someone can point me in the right direction for this. I would like to add a 'related categories' section to last leaf category in a category tree.

To explains this, lets pretend I have a category tree like this:

Electronics
> Toasters
> Televisions
> Televisions > 15" TV's
> Televisions > 17" TV's
> Televisions > 20" TV's


Now lets say, I navigate to 'Televisions', the user will be shown the three sub categories in the upper section of the site:

> Televisions > 15" TV's
> Televisions > 17" TV's
> Televisions > 20" TV's

Now when I got into the last leaf category, lets say '> Televisions > 17" TV's' we loose the categories at the top.

I'd like to show these categories, because if I have gone into the wrong category, and instead wanted to look at 15" or 20" TV's I cannot readily jump back and as part of the 'exit strategy' I'd like to show the categories.

Any suggestions on how to achieve this?

Matt

Re: In final Leaf Category, Show Same Level Categories

Posted: Sun Dec 13, 2009 1:00 pm
by montanaflynn
That would be nice, I am looking into this as well. I would like the final product page to list the categories it belongs to in the middle column too.

Re: In final Leaf Category, Show Same Level Categories

Posted: Tue Dec 15, 2009 2:46 am
by hubba
Do you meant to say you do not want the categories to be collapsable?

I think there is a module somewhere.. but I can't find it.

if anyone sees the module please point us to the right way, I am looking for that too!

Re: In final Leaf Category, Show Same Level Categories

Posted: Tue Dec 15, 2009 2:52 am
by moggiex
hubba wrote:Do you meant to say you do not want the categories to be collapsable?

I think there is a module somewhere.. but I can't find it.

if anyone sees the module please point us to the right way, I am looking for that too!
No, that's a simple hack around the IF statement in the categories module (about line 55), this is referring to displaying the children categories in a final leaf category.

I'll take stab at this tonight as I am needing it for a few sites now as exit options if the landing category is not the the right category and I hate having to hard code stuff as its a bar-steward to update later.

Matt

Re: In final Leaf Category, Show Same Level Categories

Posted: Tue Dec 15, 2009 7:25 am
by moggiex
HAX!!! I got it, took a bit of faffing but it works.

File:
/catalog/model/catalog/category.php

We need to add a new function, so at line 21 add this:

Code: Select all

	// We need to know the parent ID, so that we can work out what its equal level categories are
	public function getTotalEqualCategoriesByCategoryId($category_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$category_id . "'");
		
		return $query->row['parent_id'];
	}
This gets us the parent_id of the category we're looking at, superb, now we just need to use this function and use the results when needed.

File:
/catalog/controller/catalog/category.php

Around line: 93 look for:

Code: Select all

$category_total 		= $this->model_catalog_category->getTotalCategoriesByCategoryId($category_id);
$product_total 			= $this->model_catalog_product->getTotalProductsByCategoryId($category_id);
ADD this after it:

Code: Select all

$category_equal_total 	= $this->model_catalog_category->getTotalEqualCategoriesByCategoryId($category_id);
One more bit to alter....

just below this change:

Code: Select all

if ($category_total || $product_total) {
        		$this->data['categories'] = array();
        		
				
				$results = $this->model_catalog_category->getCategories($category_id);
To this:

Code: Select all

if ($category_total || $product_total || $category_equal_total) {
        		$this->data['categories'] = array();
        		
				
				$results = $this->model_catalog_category->getCategories($category_id);
				$results = $this->model_catalog_category->getCategories($category_equal_total);
No edits are needed for the category.tpl file, as it will appear with the categories automatically, as we've just populated with categories, even when its at its final leaf.

Happy days, considering my php and sql skillz suck :D

Matt

Re: In final Leaf Category, Show Same Level Categories

Posted: Tue Dec 15, 2009 7:38 am
by moggiex
whoops, this has an interesting side affect of showing level categories when at level 0, which is not desirable, will post some alternative code when I've stopped it from doing so.

Matt

Re: In final Leaf Category, Show Same Level Categories

Posted: Tue Dec 15, 2009 7:47 am
by moggiex
Na, stuck now,

Its party solved by putting an IF around the new request for the categories:

Code: Select all

$results = $this->model_catalog_category->getCategories($category_id);
if (!$results && $category_equal_total != 0) {
					$results = $this->model_catalog_category->getCategories($category_equal_total);
				}
However, I don't know how to make it relaise that its already at the bottom level, because the category links are coming out like this:

Tree:
> Televisions
> Televisions > 15" TV's

The links are coming out as:

Televisions > 15" TV's > 15" TV's

Which is wrong :( Sooooo close as well!

Matt