Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Page 2 of 2
Controlling sort order for categories in layered navigation
 
hyteckit
Sr. Member
 
Avatar
Total Posts:  152
Joined:  2008-04-01
Los Angeles, CA
 
witt - 02 April 2008 04:10 AM

Hi Ian,

I could not find any option to make the categories appear in the order you defined them. But you can modify /app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php.

Just add addAttributeToSort(’position’, ‘asc’) sorting criteria to the function that loads the child categories collection that will be used in the Shopping Filter block.

protected function _initItems()
{
$categoty = $this->getCategory();
$collection = Mage::getResourceModel(’catalog/category_collection’)
->addAttributeToSelect(’name’)
->addAttributeToSelect(’all_children’)
->addAttributeToSelect(’is_anchor’)
->addAttributeToFilter(’is_active’, 1)
->addAttributeToSort(’position’, ‘asc’)
->joinUrlRewrite()
->addIdFilter($categoty->getChildren())
->load();

Looks like the code addAttributeToSort(’position’, ‘asc’) is added to v1.1.2.  However, it doesn’t do a damn thing with the layered navigation (filter) on the left sidebar as the OP had asked for.

 Signature 

Current Project:
GeekGears.com - Accessories for your iPod, iPhone, MP3 Players, Cell Phone, PDA, GPS Devices, Digital Camera, and more.
Extension: Product Gallery Importer, Enhanced Search

 
Magento Community Magento Community
Magento Community
Magento Community
 
hyteckit
Sr. Member
 
Avatar
Total Posts:  152
Joined:  2008-04-01
Los Angeles, CA
 

Alright guys.  Here’s the solution.  Give me some love.

modify the file “app/code/core/Mage/Catalog/Block/Navigation.php”

replace the function:

public function getCurrentChildCategories()
    
{
        $layer 
Mage::getSingleton('catalog/layer');
        
$category   $layer->getCurrentCategory();
        
/* @var $category Mage_Catalog_Model_Category */
        
$collection Mage::getModel('catalog/category')->getCollection();
        
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
$collection->addAttributeToSelect('url_key')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('is_anchor')
            ->
addAttributeToFilter('is_active'1)
            ->
addIdFilter($category->getChildren())
            ->
joinUrlRewrite()
            ->
load();

        
$productCollection Mage::getResourceModel('catalog/product_collection');
        
$layer->prepareProductCollection($productCollection);
        
$productCollection->addCountToCategories($collection);
        return 
$collection;
    
}

with:

public function getCurrentChildCategories($sort_by ''$sort_order 'asc')
    
{
        $layer 
Mage::getSingleton('catalog/layer');
        
$category   $layer->getCurrentCategory();
        
/* @var $category Mage_Catalog_Model_Category */
        
$collection Mage::getModel('catalog/category')->getCollection();
        
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
$collection->addAttributeToSelect('url_key')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('image')
            ->
addAttributeToSelect('is_anchor')
            ->
addAttributeToFilter('is_active'1)
            ->
addAttributeToSort($sort_by$sort_order)
            ->
addIdFilter($category->getChildren())
            ->
joinUrlRewrite()
            ->
load();

        
$productCollection Mage::getResourceModel('catalog/product_collection');
        
$layer->prepareProductCollection($productCollection);
        
$productCollection->addCountToCategories($collection);
        return 
$collection;
    
}

You’ll notice I added “addAttributeToSort($sort_by, $sort_order)” and “addAttributeToSelect(’image’)”.  Wondering why I added “addAttributeToSelect(’image’)”? 

You’ll love me later, because you can now use the function getImage() from any category object, such as $category->getImage();

I use it to create thumbnails for all my categories.

Here are the uses of the new getCurrentChildCategories() function:

sort_by - what attribute you want to sort by, such as ‘name’ or ‘position’
sort_order - ‘asc’ or ‘desc’. defaults to asc (ascending order)

examples:

getCurrentChildCategories() - nothing gets sorted
getCurrentChildCategories(’’name”, “desc") - sort by name in descending order
getCurrentChildCategories(’’position") - sort by position in ascending order, like you see in the category listing under the magento admin

OKAY. So how do I make it work in the layered navigation (filter)?

Under the theme (skin) you are using.  Modify the file “template/catalog/navigation/left.phtml”

Update:

<?php $_categories=$this->getCurrentChildCategories() ?>

To:

<?php $_categories=$this->getCurrentChildCategories('position'?>

or whatever order you want to sort by.

Okay, give me so love.

 Signature 

Current Project:
GeekGears.com - Accessories for your iPod, iPhone, MP3 Players, Cell Phone, PDA, GPS Devices, Digital Camera, and more.
Extension: Product Gallery Importer, Enhanced Search

 
Magento Community Magento Community
Magento Community
Magento Community
 
vgration
Member
 
Total Posts:  46
Joined:  2008-07-09
 

works great. now how do you make the same change on the dropdown menus?

 
Magento Community Magento Community
Magento Community
Magento Community
 
hyteckit
Sr. Member
 
Avatar
Total Posts:  152
Joined:  2008-04-01
Los Angeles, CA
 
vgration - 11 August 2008 01:22 PM

works great. now how do you make the same change on the dropdown menus?

The dropdown menus calls another function.  Rewrite the dropdown menu function to use:

$_categories=$this->getCurrentChildCategories('position')

Your question is solved.

 Signature 

Current Project:
GeekGears.com - Accessories for your iPod, iPhone, MP3 Players, Cell Phone, PDA, GPS Devices, Digital Camera, and more.
Extension: Product Gallery Importer, Enhanced Search

 
Magento Community Magento Community
Magento Community
Magento Community
 
vgration
Member
 
Total Posts:  46
Joined:  2008-07-09
 

its not that straight forward. in top.phtml you have

<div class="header-nav-container">
    <
div class="header-nav">
        <
h4 class="no-display"><?php echo $this->__('Category Navigation:'?></h4>
        <
ul id="nav">
        
<?php foreach ($this->getStoreCategories() as $_category): ?>
            <?php 
echo $this->drawItem($_category?>
        <?php 
endforeach ?>
        
</ul>
    </
div>
    
<?php echo $this->getChildHtml('topLeftLinks'?>
</div>
I chenged
<?php foreach ($this->getStoreCategories() as $_category): ?>
to
<?php foreach ($this->getCurrentChildCategories('name') as $_category): ?>
When i change that the site goes funky. The top menu is replaced with all the child categories sorted and the home page gives a fatal error undefined function getChildren. I will keep working on it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
hallowed
Jr. Member
 
Total Posts:  21
Joined:  2008-08-19
 

I have had no luck with changing the codes.

I changed them to what was said and nothing changed whether it be on the index or in the admin panel.

Is there something I am missing?

 
Magento Community Magento Community
Magento Community
Magento Community
 
vgration
Member
 
Total Posts:  46
Joined:  2008-07-09
 

it quit working for me after the last updates so i just decided to not worry about it for now.

 
Magento Community Magento Community
Magento Community
Magento Community
 
hallowed
Jr. Member
 
Total Posts:  21
Joined:  2008-08-19
 

Always nice to know that updates make things worse.

I have not created a functional store yet, this is all for experimentation and custom theme creation so I can delete the categories and re-do them so they are in order on the main page but I expect this to be a little more user friendly than this.

I know this is a new cart but they consider it revolutionary, well with everything hidden in a ton of folders with no real instruction manual it has only been a revolution in confusion.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tarunjot K
Jr. Member
 
Total Posts:  4
Joined:  2008-09-22
 

Hi Ian.Can u please tell how to control the order of the sub categories in the drop-down of category ‘Fruits’?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nick Weisser
Member
 
Avatar
Total Posts:  37
Joined:  2007-09-03
Zurich, Switzerland
 
hyteckit - 05 August 2008 04:11 AM

You’ll notice I added “addAttributeToSort($sort_by, $sort_order)” and “addAttributeToSelect(’image’)”.  Wondering why I added “addAttributeToSelect(’image’)”? 

You’ll love me later, because you can now use the function getImage() from any category object, such as $category->getImage();

I use it to create thumbnails for all my categories.

Good stuff, hyteckit!

I tried to figure out how to create thumbnails for my sub categories, but it’s not as easy at it seems. Did you extend class Mage_Catalog_Helper_Image in order to achieve this?

Any pointers would be very much appreciated.

 Signature 

Magento Premium Hosting with Virtualmin Control Panel.

 
Magento Community Magento Community
Magento Community
Magento Community
 
firespire
Jr. Member
 
Total Posts:  30
Joined:  2008-02-24
Cape Town, South Africa
 

hyteckit, thank you and you rock man!
tell me though, is there anyway we can add this as a module to have a field in the admin panel that says : sort order and you specify a value to the category..?
also another thing I am wondering, and this might be completely of the topic, please excuse me if it is, is i want to make a dynamic sidebar (left coulumb) so that when you are on a product view page and you have the vertical menu on the left pulling in “products” associated with a category, how to I manipulate the order of those “products”?

once again, this whole community rocks man, and the application is so cool!

wins

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 2
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
53184 users|763 users currently online|107213 forum posts