Wednesday, March 19, 2014

Magento : Sort By Newest Product In Category Page

Create a custom module. Suppose, Bd_Sortbynew

Write following code in config.xml
<?xml version="1.0"?>
<config>
<modules>
<Bd_Sortbynew>
<version>0.1.0</version>
</Bd_Sortbynew>
</modules>
<global>
<resources>
<sortbynew_setup>
<setup>
<module>Bd_Sortbynew</module>
                    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</sortbynew_setup>
</resources>
    </global>
</config>

Then create a sql installer file with following code
$installer = $this;
$installer->startSetup();
$prodEntityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->updateAttribute($prodEntityTypeId, 'created_at', 'frontend_label', 'New');
$installer->updateAttribute($prodEntityTypeId, 'created_at', 'used_for_sort_by', 1);
$installer->endSetup();

For revert you can use following script

require 'app/Mage.php';
Mage::app();

$eavAttribute = new Mage_Eav_Model_Mysql4_Entity_Attribute();
$attribute_id = $eavAttribute->getIdByCode('catalog_product', 'created_at');
if($attribute_id) {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql  = "update catalog_eav_attribute SET used_for_sort_by = 1 where attribute_id = '$attribute_id' limit 1";
$write->query($sql);
} else {
echo 'Error!';
}

1 comment:

  1. Hi!

    could you please specify where to create the files?

    thank you!

    ReplyDelete