tags:

views:

316

answers:

2

I'm setting up a new store using 1.4.1

I'm trying sort the products in the list by the subcategories they belong to. At the top of list.phtml is

$_productCollection=$this->getLoadedProductCollection();

I tried adding sort filters to that by adding the line

$_productCollection->setOrder('category_ids', 'asc')->setOrder('name', 'asc');

I also tried addAttributeToSort instead of setOrder. This doesn't seem to have any effect. I'm guessing that $_productCollection is not a model I can sort in this manner. I have been digging around trying to find the correct place to apply the sort method without any success.

Can someone tell me the proper place to do this?

A: 

Where is the call to getLoadedProductCollection coming from? It's a safe bet (by the name of the collection), that the collection has been loaded (e.g. the SQL has already been executed) by the time you get the collection back. This means that your opportunity for limiting/sorting is over with.

There may be another version of that method that hasn't already been loaded. Use that one if possible.

Thanks, Joe

Joseph Mastey
Shouldn't there be a way to sort the collection after the SQL has been executed? I think if I used something other than getLoadedProductCollection than I would have to rewrite the way that the product listing works.
dardub
The concept of sorting after the collection has already been loaded is problematic. Think of an install with 5k products. The collection of the first 30 products has AA thru AK. If you sort them in reverse order, what most people think of mentally is getting ZZ-ZA, but you would actually get AK to AA. If you want to sort manually, copy them to an array and use ksort.
Joseph Mastey
A: 

You right Joseph, the SQL are into /app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php, search by "public function addAttributeToSort"

But the code to do that sort is out of my reach.

Only remember to save de new Collection.php on the path /app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Product to avoid problems on updates.

If you realise de code, please post.

I'm trying from here.

Reegards

Leonardo Di Sarli

Leonardo Di Sarli