When displaying a Magento category is it possible to have any new products (products that have a "new from" and "new to" date) listed at the beginning of the collection so they show up on the first page of the category instead of the default location based on the current sort?
Would it be开发者_开发问答 necessary to have two queries?
Thanks!
Code to sort products by news_from_date and news_to_date:-
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$productCollection->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');
You can view the new products code at the class Mage_Catalog_Block_Product_New
(app/code/core/Mage/Catalog/Block/Product/New.php)
Hope this helps.
精彩评论