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

CUSTOM SORT FIELD FOR MAGENTO PRODUCTS LISTING


Today we are going to change Magento default products displaying order. I mean “sort by” field and it’s direction. This article will show how to add a new sorting field, called “Date added” to the products list. First of all, we need to create a new module. Don’t worry, it is going to be a very simple extension. This way our customization will work after Magento upgrade. So we just need to rewrite Mage_Catalog_Model_Config model class and Mage_Catalog_Block_Product_List_Toolbar block class. Module configuration file app/code/local/Atwix/Tweaks/etc/config.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_Tweaks>
            <version>1.0</version>
        </Atwix_Tweaks>
    </modules>
    <global>
        <blocks>
            <tweaks>
                <class>Atwix_Tweaks_Block</class>
            </tweaks>
            <catalog>
                <rewrite>
                    <product_list_toolbar>Atwix_Tweaks_Block_Product_List_Toolbar</product_list_toolbar>
                </rewrite>
            </catalog>
        </blocks>
        <models>
            <catalog>
                <rewrite>
                    <config>Atwix_Tweaks_Model_Catalog_Config</config>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>
In Atwix_Tweaks_Block_Product_List_Toolbar we just define default direction,app/code/local/Atwix/Tweaks/Block/Product/List/Toolbar.php:
1
2
3
4
5
<?php
class Atwix_Tweaks_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
    protected $_direction = 'asc';
}
In Atwix_Tweaks_Model_Catalog_Config class we rewrite getAttributeUsedForSortByArray function to add custom sorting fields, app/code/local/Atwix/Tweaks/Model/Catalog/Config.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
class Atwix_Tweaks_Model_Catalog_Config extends Mage_Catalog_Model_Config
{
    public function getAttributeUsedForSortByArray()
    {
        $options = array(
            'created_at' => Mage::helper('catalog')->__('Date Added')
        );
        foreach ($this->getAttributesUsedForSortBy() as $attribute) {
            $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
        }
        return $options;
    }
}
Note, that you can add your own fields to $options array. The last step is to enable our module,app/etc/modules/Atwix_Tweaks.xml:
1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_Tweaks>
            <active>true</active>
            <codePool>local</codePool>
        </Atwix_Tweaks>
    </modules>
</config>
Now you can see changes at the frontend. The source code can be found here. We hope that this post will help you with coding. That’s all for now, stay tuned.

courtesy : https://www.atwix.com/magento/custom-sorting-product-listing/