You are here:  » Filter Category Order While Using Category Hierarchy


Filter Category Order While Using Category Hierarchy

Submitted by tommo101 on Wed, 2022-02-23 12:41 in

I've noticed that while using category hierarchy, that the order of categories in the category filter isn't alphabetical, but instead done by categoryid, which means when people look at the list of categories, they are all over the place.

Is there a way that I can make this order alphabetically the way it does when using the standard category system?

So for the normal category filter the line of code in the searchfilter file is:

$sql1 = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." AND category <> '' ORDER BY category";

Which has the "ORDER BY category" bit...

Where as for category hierarchy it is this:

$sql1 = "SELECT DISTINCT(categoryid) FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." AND categoryid > 0";

Which doesn't really allow for a nice neat ORDER BY function.

So you can see what I mean, if you look at my filters, brand and retailer are nicely in alphabetical order, but categories is a bit unorganised: {link saved}

Submitted by support on Wed, 2022-02-23 14:06

Hi,

They should be sorted subsequently of that initial query by the tapestry_categoryHierarchyArray() function - do you see the following code at line 78 of the same file:

            $categories = tapestry_categoryHierarchyArray($categoryids);

If not, please could you copy the surrounding code from the section within the IF condition;

if ($config_useCategoryHierarchy)

...as there have been various itterations of the category hierarchy code over the years and I'll check it out further for you (+if you could confirm the version you are using...)

Thanks,
David.
--
PriceTapestry.com

Submitted by tommo101 on Wed, 2022-02-23 14:26

Hi there,

As you can probably tell, it's a heavily modified version of the script these days and in all honesty, i'm not actually sure exactly which version it is any more. The support info page says 15/09A (Beta), but I know i've downloaded more up to date versions since then and have juggled bits around and tinkered no end.

Anyway, this is what my searchfilters file currently looks like:

{code saved}

Submitted by support on Wed, 2022-02-23 14:32

Thanks,

In this case it should be resolved by sorting the return value from tapestry_categoryHierarchyArray() so in your version at line 63:

                    $categories = tapestry_categoryHierarchyArray($categoryids);

...REPLACE with:

                    $categories = tapestry_categoryHierarchyArray($categoryids);
                    asort($categories);

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Wed, 2022-02-23 14:41

Thanks for the super quick response. I've done that replacement and it does kinda of sort it, but only as per the hierarchy, so each top level category has all the sub categories in alphabetical order, but when it brings up the next top category it starts at the beginning of the alphabet again.

Not sure if that explains what I mean well, but if you look at the drop down, you'll see what I mean:

{link saved}

Submitted by support on Thu, 2022-02-24 09:33

Ah, that is the natrual sort with the full path names but you could make the list lowest level category names only and that would place them entirely in alphabetical order. To try this use the following alternative replacement to the above;

  $categories = tapestry_categoryHierarchyArray($categoryids);
  foreach($categories as $k => $v)
  {
    $categories[$k] = array_pop(explode("/",$v));
  }
  asort($categories);

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Thu, 2022-02-24 10:45

You sir are a legend... Absolutely perfect and saved me so much time.... :) Thank you so much.