Thursday, April 26, 2012

Magento: Sort latest product by ‘created date’


Use following code to sort/order product by created 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');

4 comments:

  1. Anyway to sort newest products by price instead?

    Reply Delete
    Replies
    1. Hey,

      Insert the following code at last:
      $collection->setOrder('price','DESC'

      Delete
  2. This comment has been removed by the author.

    Reply Delete
  3. This comment has been removed by the author.

    Reply Delete

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /