2

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

1

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.

  • Really cool answer I appreciate it. And it looks like `Mage_Catalog_Block_Layer_State``is het way ot go in our case. 1 last question. When using getActiveFilters .... would it be possible to obtain variables via the layout update (passing along the variable( that we want to put on top for instance via a param that this function can read? (so what remains unclear is how does one pass in the variables that influence sorting based on some standard GUI methods) – snh_nl Jan 4 '16 at 12:42
  • Yes, set variables in the layer_view block with <action method="setData"><key>foo</key><value>bar</value></action> and access them like this: $this->getLayout()->getBlock('catalog.leftnav')->getData('foo') – Fabian Schmengler Jan 4 '16 at 12:45

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.