Categories
Magento PHP

Sort product by newest in Magento (with out hacked!)

After adding a few products in my Magento 1.4.0.1 site, I have found that the latest added product will go to the last of the product list. That doesn’t make sense. So… I googled it for finding the solution but almost of them are hacking the Magento’s core code. This will cause my website some problems when upgrade Magento in the future.

The right solution is try to config Magento using layout files.
In this case, product ordering, you have to open “catalog.xml” in the layout folder. Then in the following code block, you just add the red line in order to change the default sorting direction to descending.

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
 <block type="page/html_pager" name="product_list_toolbar_pager"/>
  <!-- The following code shows how to set your own pager increments -->
 <!-- .... -->
 <action method="setDefaultDirection"><dir>desc</dir></action>
 </block>
<block type=”catalog/product_list_toolbar” name=”product_list_toolbar” template=”catalog/product/list/toolbar.phtml”>
<block type=”page/html_pager” name=”product_list_toolbar_pager”/>
<!– The following code shows how to set your own pager increments –>
<!–
<action method=”setDefaultListPerPage”><limit>4</limit></action>
<action method=”setDefaultGridPerPage”><limit>9</limit></action>
<action method=”addPagerLimit”><mode>list</mode><limit>2</limit></action>
<action method=”addPagerLimit”><mode>list</mode><limit>4</limit></action>
<action method=”addPagerLimit”><mode>list</mode><limit>6</limit></action>
<action method=”addPagerLimit”><mode>list</mode><limit>8</limit></action>
<action method=”addPagerLimit” translate=”label”><mode>list</mode><limit>all</limit><label>All</label></action>
–>
<action method=”setDefaultDirection”><field>desc</field></action>
</block>

After that upload this file to your server.  However, don’t forget to clear Magento’s cache unless you will see the result.

Note:

You may change method attribute’s value to an another one. This value is the name of methods of class Mage_Catalog_Block_Product_List_Toolbar [click here for the complete API document]. And also you can change the value of dir block (<dir>…</dir>) which is the argument name and its value of the method

Reference:
CK Rock @ comment #59 of this thread

9 replies on “Sort product by newest in Magento (with out hacked!)”

Is there a way to just want to change the default value (0) of the “product listing sort by” Best Value, to a higher value?

Leave a Reply

Your email address will not be published. Required fields are marked *