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
Why Google Hates Black Hat SEO: How Unethical Tactics Damage Search Engines
What is Black Hat SEO? Black hat SEO is a set of dishonest techniques used to boost a website’s search engine ranking. U...
Essential WooCommerce Security Plugins and Tips for a Secure Store
Are you looking for the best wooCommerce plugins for your secure store? If you keep reading this blog, we will show you the m...
How to Improve Your Ecommerce Website’s User Experience for Higher Conversions
Creating a good user experience on your e-commerce website is important for driving conversion. Every user expects a fast and...
How Virtual Shopping is Shaping the Future of E-commerce?
Virtual reality has changed so many businesses, including e-commerce. Virtual shopping is becoming a groundbreaking trend tha...