1

Basically Magento attribute option takes database order by default if we provide position it will shows based on that order. Like below

 color:  blue
            green
            black

    size:   s
            xl
            M
    housing: 9
             5
             99
             10
             bold
             ancle
             raise
             home

Like this I created Option same way it will in front end layered navigation

But my requirement is need to show in front end like below

color: black
       blue
       green
size:  m
       s
       xl


housing:     5
                 9
                 10
                 99
                 ancle
                 bold
                 home
                 raise

like this I need I don't want to provide position for every attribute option because I have lot of options having some attributes, so I am planing to display alphabetically, how admin created I don't care.

If anyone knows please help me where to change this order

3
+25

Sorting can be done at the template level, but my preference would be to handle this in the block level.

To get to the right place, start at the template, like here:

File: app/design/frontend/base/default/template/catalog/layer/filter.phtml

<?php foreach ($this->getItems() as $_item): ?>

The call chain looks like this:

1. (36): app/design/frontend/base/default/template/catalog/layer/filter.phtml
2. (122): Mage_Catalog_Model_Layer_Filter_Abstract->getItems()
3. (120): Mage_Catalog_Model_Layer_Filter_Abstract->_initItems()

So from the template we end up in Mage_Catalog_Model_Layer_Filter_Abstract::_initItems:

protected function _initItems()
{
    $data = $this->_getItemsData();
    $items=array();
    foreach ($data as $itemData) {
        $items[] = $this->_createItem(
            $itemData['label'],
            $itemData['value'],
            $itemData['count']
        );
    }
    $this->_items = $items;
    return $this;
}

We can see how they are generated, using method $this->_createItem. This is the perfect point to inject our override. But on an abstract class, we can't quite get to it without a core/local pool replacement (not ideal).

Suggested Solution

  1. Create a block rewrite on catalog/block_layer_filter_attribute
  2. Implement overriding method getItems on your rewrite class
  3. Implement sorting in your rewrite class on items

So my class might look like this:

class Namespace_Module_Block_Catalog_Layer_Filter_Attribute
    extends Mage_Catalog_Block_Layer_Filter_Attribute
{

    protected function _sortItems(
        Mage_Catalog_Model_Layer_Filter_Item $a,
        Mage_Catalog_Model_Layer_Filter_Item $b
    ) {
        return strnatcmp($a->getLabel(), $b->getLabel());
    }

    public function getItems()
    {
        $result = parent::getItems();

        usort($result, array($this, '_sortItems'));

        return $result;
    }

}

I should note that this only applies to the generic attribute filter. Magento implements custom filters also for price, decimal, and category types. If you need to sort options on those, too, then create additional block rewrites to sort in a similar way.

  • This is for values having names only for me having alphanumaric.The above one i got it before it self but while alpha numaric time i am unable to get – Pawankumar Sep 6 '17 at 20:29
  • See my updated answer: I improved the sorting using strnatcmp, which should account for both numbers and letters in the way you are expecting. – Rick Buczynski Sep 6 '17 at 20:37
  • Hi Do I need add anything also to filter.phtml? I have this <?php foreach ($filterItems as $filterItem): ?> no getthis – BartZalas May 19 '18 at 11:28
  • No. Not if you extended the block as I described, because sorting will occur before your template renders each item. Are you sure your rewrite is active? – Rick Buczynski May 19 '18 at 12:27
  • I created app/code/myvendor/mymodule/Model/Layered/SortLayered.php (with code above) and app/code/myvendor/mymodule/etc/module.xml but I think I'm mising some xml file with rewrite for catalog/block_layer_filter_attribute – BartZalas May 20 '18 at 8:52
0

To sort the option alphabetically, you should find the render file for these option. In base theme you find them in below path.

app/design/frontend/base/default/template/catalog/layer

Similarly, in your theme locate this file and sort the object which is containing the option.

  • i find it but i am unable to change the sort by name how to change that can you tell me – Pawankumar Sep 5 '17 at 5:19
  • filter .phtml file $this->getItems() like this having – Pawankumar Sep 5 '17 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.