Zachary Beschler
Zachary Beschler

Reputation: 126

Magento - set custom sort by and order direction with Layout Updates for just one category

What I am trying to do seems very simple...but I haven't yet been able to figure this out for some reason.

What I would like to do is—for just one particular category—change the sort by attribute and sort order direction via the Magento backend. That said, I'm thinking the best thing to do would be to use custom Layout Update XML. After searching extensively on the internet, I cannot for the life of me seem to find an answer (I'm hoping an answer to this will will help others too).

Very simply, I've tried the following custom Layout Update XML for one specific category:

<reference name="product_list_toolbar">
    <action method="setDefaultOrder"><string>created_at</string></action>
    <action method="setDefaultDirection"><string>desc</string></action>
</reference>

And it just doesn't work! (It simply does nothing in both my custom theme, and in the default RWD theme.)

Is what I'm trying to do impossible? It seems so easy!

Thanks in advance.

Note: I am using Magento version 1.9.1.1

Upvotes: 1

Views: 4818

Answers (3)

Thoras
Thoras

Reputation: 11

This worked for me:

<reference name="product_list">
    <action method="setDefaultDirection"><string>desc</string></action>
    <action method="setSortBy"><string>sku</string></action>
</reference>
<reference name="product_list_toolbar">
    <action method="setDefaultDirection"><string>desc</string></action>
    <action method="setSortBy"><string>sku</string></action>
    <action method="setDefaultGridPerPage"><limit>260</limit></action>
    <action method="setDefaultListPerPage"><limit>260</limit></action>
</reference>

Upvotes: 1

Zachary Beschler
Zachary Beschler

Reputation: 126

Found this after a lot of trial and error.

To change sort order, I used Ricky's code and, using the Magento backend, added it as layout update XML for that category:
(Category > Custom Design > Custom Layout Update)

<reference name="product_list_toolbar">
    <action method="setDefaultDirection"><string>desc</string></action>
</reference>

To add sort by items, it requires extension of the Mage_Catalog_Model_Config class, as such:
(specifically to rewrite the function getAttributeUsedForSortByArray())

public function getAttributeUsedForSortByArray()
{
    $options = array(
        'position'  => Mage::helper('catalog')->__('Position'), // ADD TRAILING COMMA
        // ADD ADDITIONAL ATTRIBUTES BELOW
        'created_at'  => Mage::helper('catalog')->__('Date')
    );
    foreach ($this->getAttributesUsedForSortBy() as $attribute) {
        /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
        $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
    }

    return $options;
}

Upvotes: 2

Ricky Odin Matthews
Ricky Odin Matthews

Reputation: 373

For some reason, the custom layout method doesn't work in Magento 1.9 and up. This is probably caused by Magento overwriting it with the <catalog_category_default> tags in local.xml The only solution I've found, is to open your themes local.xml, and insert the following;

<CATEGORY_24>
     <reference name="product_list_toolbar">
        <action method="setDefaultDirection"><string>desc</string></action>
    </reference>
</CATEGORY_24>

Where <CATEGORY_24> is replaced by the Category ID.

Upvotes: 2

Related Questions