Benubird
Benubird

Reputation: 19477

How to programmatically get magento category sort order

I am getting a collection of categories using

foreach(Mage::getModel('catalog/category')->getCollection() as $category)

For each of these categories, I need to retrieve the sort order. How do I do this?

The only function I can find that looks relevant in the category class is getDefaultSortBy(), which always returns news_from_date, which is neither the default sort order (position) or the selected sort order (price), so I don't know where that's getting its value from. I also noticed that if, in the magento backend, I change the list of available sort orders, the function getAvailableSortByOptions still always returns the same array. From these two facts, I conclude that the category functions must be accessing some sort of cross-category global settings, which is useless to me.

I want the specific sort order chosen for each specific category. Is there any way to retrieve this? Or do I need to write my own SQL? In that case, which table do I need to query?

I am using magento enterprise ver. 1.11.1.0

Upvotes: 1

Views: 4455

Answers (1)

Benubird
Benubird

Reputation: 19477

Found the answer. Naturally, moments after I posted the question!

The problem is that Mage::getModel('catalog/category')->getCollection() does NOT automatically load all of the category attributes. You have to indicate which ones to retrieve manually. So, I need to replace this:

foreach(Mage::getModel('catalog/category')->getCollection() as $category)

With this:

foreach(
    Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('default_sort_by')
    ->addAttributeToSelect('available_sort_by')
    as $category
)

Upvotes: 1

Related Questions