Home » Magento7 June 2011

Magento: Sort latest product by ‘created date’ and ‘new from date’

Here is a quick tip to sort/order latest product by both created date and new from date.

Created At date is automatically added when you add a new product. However, you have to set New from Date manually while adding or editing product.

You may have product with same new from date. And, then you need created at date to sort the latest product.

Here is the code to sort/order product by both created date and new from date:-

Hope this helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • bim

    Hi, I wanted to call the new products block outside the magento folder, I wqas wandering if you can help me with it? thanks!

  • Hi, could you give me advice how to sort random attribute array? Example:
    I got a block at home page where must be displayed 3 Products according 2 attributes. First attribute is simply to get this product to be shown at start page … lets say ‘show_at_homepage’. 
    There is a second attribute to deal with – e.g. ‘color’.

    $collection = Mage::getModel(‘catalog/product’)->getCollection(); $collection->addAttributeToSelect(‘show_at_homepage’); $collection->addFieldToFilter(array( array(‘attribute’ => ‘show_at_homepage’, ‘eq’ => true), ));

    Now the colors are the drop down values from attribute color:
    ]
    $colors = array(‘1’ => ‘398’,’2′ => ‘399’,’3′ => ‘397’);

    I can’t use at this place addAttributeToSort() methode. And I need it to be sorted exactly as I wrote it in $colors array: 1->398, 2->399 and 3->397.

    Could you say me the way? Appriciate for advice. Thx!

  • Suzerainsalman

    hiii … where to add this code

  • Lex

    This is working great. But is it also possible to add the layer navigation based on a custom product collection?

  • varsha

    can u explain this code

  • Ronnie

    Magento: Sort latest product by ‘created date’ and ‘new from date’
    Here is the code to sort/order product by both created date and new from date:-
    $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); $collection = Mage::getModel(‘catalog/product’) ->getCollection() ->addAttributeToFilter(‘news_from_date’, array(‘date’ => true, ‘to’ => $todayDate)) ->addAttributeToFilter(‘news_to_date’, array(‘or’=> array( 0 => array(‘date’ => true, ‘from’ => $todayDate), 1 => array(‘is’ => new Zend_Db_Expr(‘null’))) ), ‘left’) ->addAttributeToSort(‘news_from_date’, ‘desc’) ->addAttributeToSort(‘created_at’, ‘desc’);
    Hi, friend
    please tell me where to add this code?

  • Hi
    How to sort product by best sellers in product list page toolbar?

  • six3

    Hi, where to place the code?

  • nilesh

    i want to display product automatically of it’s creation date till 30 days. without using admin to set date . plz help me

  • Alexander

    Hi, Please let me know where i need to place the code