Magento – Magento category layout update: Set “Position of attribute in layered navigation block”

attributescategorylayered-navigationlayoutlayout-update

Magento attributes sort order (position) can be set on the attribute level using the element Position of attribute in layered navigation block.

We have some categories that are built bassed on a specific attribute. Normally size is the 6tc position/order of atrtibute filters in layered navigation.

For one category I would like to move the "Position of attribute in layered navigation block" to say position 1 (top).

Question: can this be done via a category layout update?

thanks, Sean

enter image description here

Best Answer

Not without custom code. There is a block class for the filter display, Mage_Catalog_Block_Layer_State, but the filter data comes from the model Mage_Catalog_Model_Layer_State, which is populated by Mage_Catalog_Model_Layer and cannot be influenced from the block.

If your alternative sort order can be expressed with an SQL order clause, you could override Mage_Catalog_Model_Layer::_prepareAttributeCollection() and change the sort order of the collection depending on the current category. It's set to $collection->setOrder('position', 'ASC') before this method is called. But rewriting the layout model is better avoided.

A better option without rewrites is to create a custom block that extends Mage_Catalog_Block_Layer_State:

class Stack_Catalog_Block_Layer_State extends Mage_Catalog_Block_Layer_State
{
    public function getActiveFilters()
    {
        $filters = parent::getActiveFilters();

        // $filters is an array of Mage_Catalog_Model_Layer_Filter_Item objects
        // sort it with usort() to your needs

        return $filters;
    }
}

And another one that extends Mage_Catalog_Block_Layer_View because this is where the state block is created:

class Stack_Catalog_Block_Layer_View extends Mage_Catalog_Block_Layer_View
{
    protected function _initBlocks()
    {
        parent::_initBlocks();
        $this->_stateBlockName = 'stack_catalog/layer_state';
    }
}

Then replace the layer block in your categories custom layout update:

<reference name="left">
    <block type="stack_catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>

It's adding a new block, but using the same name catalog.leftnav of the original block, so it will be replaced. This is because you cannot change the type of an existing block.