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
From Text to Reality: Unveiling the Possibilities of AI-Generated Video with Sora
OpenAI, already renowned for pushing the boundaries of artificial intelligence, has done it again! This time, they've unveile...
Hugging Face: A New AI Tool To Transform Images With A Prompt
In today’s tech world Artificial Intelligence stands out as the top phases when compared to other tools. Now here’s the n...
Tired of stale Stories? Spice things up with Instagram’s NEW custom “Add Yours” templates
In the ever-evolving realm of social media, Instagram continues to lead the way with innovative features. The latest addition...
Google lets advertisers opt out of Search Partner Network amid Adalytics claims
Google is now offering advertisers the choice to refrain from participating in the Search Partner Network (SPN). This move...