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:
$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');
Hope this code will help you. Thanks.
Recent Posts
Want to know what people are searching for in real time?
What is Google Trends? Google Trends is a useful tool that shows how popular search topics are on Google over time. It hel...
Why Are Google Reviews Disappearing? Keep an Eye on them
Are your Google reviews disappearing? Discover why reviews may go missing, how to resolve the issue, and what to do if your G...
Meta Announces New Personalization Elements for Meta AI
Meta is working on improving its AI assistant by adding new memory features. This will allow the assistant to remember things...
What is DeepSeek? Unlocking the Power of AI in Fluency.
DeepSeek is a Chinese AI startup that focuses on creating advanced language models (LLMs) capable of strong reasoning and pro...
