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
Google released an app called AI Edge Gallery that lets you download and run AI models locally
Google has quietly launched a new experimental app called AI Edge Gallery. This app allows people using Android phones to run...
YouTube Expands Voice Replies, Adds Shorts Quiz Sticker
YouTube now has a new quiz sticker for Shorts, and it's also expanding the use of voice replies on clips, giving viewers anot...
Smart Hashtags: How IQ Hashtags Can Boost Your Engagement on Social Media
What is IQ Hashtags? IQ Hashtags is a useful tool for analyzing social media. It helps users improve their hashtag use on ...
Google Launches What’s Happening Feature for Business Profiles to Promote Events
Google is introducing a new feature called "What's Happening" for Google Business Profiles. It’s designed to help bars and ...
