0

I need on load product order newest and order desc , right now I need to filter it manually and for direction I need to click on arrow twice at product listing....Is there any way to do it programmatically ?

can u change following of catalog/product/list.phtml...that i get newest product and in desc order ?

$_productCollection->clear()
                    ->getSelect()
                    ->reset(Zend_Db_Select::ORDER)
                    ->joinLeft(array('cpei'=>'catalog_product_entity_int'),     'e.entity_id = cpei.entity_id AND cpei.attribute_id = 197 AND cpei.store_id = 0', 'cpei.value as product_on_top' )
                    ->order(array('product_on_top DESC', 'price_index.min_price ASC', 'final_price ASC'));

2 Answers 2

2

You can change the direction from layout XML of your theme.

Below is the code of the local.xml file for your reference.

<?xml version="1.0"?>
<layout version="0.1.0">
    <!-- Change default direction for simple searches -->
    <catalogsearch_result_index>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><dir>desc</dir></action>
        </reference>
    </catalogsearch_result_index>

    <!-- Change default direction for advanced searches -->
    <catalogsearch_advanced_result>
        <reference name="search_result_list">
            <action method="setDefaultDirection"><dir>desc</dir></action>
        </reference>
    </catalogsearch_advanced_result>

    <!-- Change default direction for a category without layered navigation -->
    <catalog_category_default>
        <reference name="product_list">
            <action method="setDefaultDirection"><dir>desc</dir></action>
        </reference>
    </catalog_category_default>

    <!-- Change default direction for a category with layered navigation -->
    <catalog_category_layered>
        <reference name="product_list">
            <action method="setDefaultDirection"><dir>desc</dir></action>
        </reference>
    </catalog_category_layered>

    <!-- Change default direction for category with ID 3 -->
    <CATEGORY_3>
        <reference name="product_list">
            <action method="setDefaultDirection"><dir>desc</dir></action>                                   
        </reference>
    </CATEGORY_3>
</layout>

Also, you can add layout XML as a Custom Design of your category in the admin. This setting will overwrite the local.xml configuration.

You can use the below code to get the list of products from a store.

$_productCollection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSort('created_at', 'DESC')
                        ->addAttributeToSelect('*')
                        ->load();
foreach ($_productCollection as $_product){
   echo $_product->getName();
}

Hope it helps!!!

6
  • This change the arrow direction but what i need is when it clicked can you see change in url "?dir=desc&order=product_id" this is what i need on load. Commented Aug 28, 2019 at 6:51
  • 1
    For this, you need to override the product collection and make set order to newest. I've updated my answer for your refrence.
    – Sumit
    Commented Aug 28, 2019 at 6:56
  • Will you help me or suggest me any link for it ? Commented Aug 28, 2019 at 7:09
  • 1
    You can try this to override product collection on the category page and add your customizations there magemonkeys.com/overriding-model-in-magento-1-9-x.
    – Sumit
    Commented Aug 28, 2019 at 7:28
  • 1
    Sumit's answer should be marked as the correct solution. Commented May 14, 2020 at 19:48
0

just add below code in catalog/product/list.phtml

$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
$_productCollection->clear()
                ->getSelect()
                //->reset(Zend_Db_Select::ORDER)
                //->joinLeft(array('cpei'=>'catalog_product_entity_int'),     'e.entity_id = cpei.entity_id AND cpei.attribute_id = 197 AND cpei.store_id = 0', 'cpei.value as product_id' )
                ->order(array('product_id DESC'));
$_category  = Mage::getSingleton('catalog/layer')->getCurrentCategory();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.