Thursday, June 30, 2016

Magento - Sort by Date Added

It is quite easy to add a sorting by date option if you're OK (you shouldn't be) with modifying core files. Simply modify app/code/core/Mage/Catalog/Model/Config.php file as in example below:
public function getAttributeUsedForSortByArray()
{
    $options = array(
        'position'  => Mage::helper('catalog')->__('Position'),

        // HERE IS OUR NEW OPTION
        '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;
}

It is not so easy, if you are not into modifying core files. In that case you must create this bunch of files:
app/etc/modules/Stackoverflow_Catalog.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Stackoverflow_Catalog>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Stackoverflow_Catalog>
    </modules>
</config>
app/code/local/Stackoverflow/Catalog/etc/config.xml
<?xml version="1.0"?>
<config>
    <global>
        <models>
            <catalog>
                <rewrite>
                    <config>Stackoverflow_Catalog_Model_Config</config>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>
app/code/local/Stackoverflow/Catalog/Model/Config.php
<?php

class Stackoverflow_Catalog_Model_Config extends Mage_Catalog_Model_Config {
    public function getAttributeUsedForSortByArray() {
        $options = parent::getAttributeUsedForSortByArray();
        if (!isset($options['created_at'])) {
            $options['created_at'] = Mage::helper('catalog')->__('Date');
        }
        return $options;
    }
}
TIP: Go for a clean way, it will pay of in a long run.
courtesy:http://stackoverflow.com/a/7202492/6409655

No comments:

Post a Comment