2011-03-18 79 views
3

我正试图在我的magento商店上展示最新产品。我如何订购Magento的最新产品?

什么是近期产品?它由两个标准定义:

  • created_at
  • news_from_date

我想订created_at & news_from_date之间漠然我的产品,而不是一个标准的第一个,第二个后。

任何想法?

我已经尝试下面的代码:

->addAttributeToSort('created_at, news_from_date', 'desc') 

OR

->addAttributeToSort('news_from_date', 'desc') 
->addAttributeToSort('created_at', 'desc') 

OR

->addAttributeToSort(array('created_at' => 'desc', 'news_from_date' => 'desc')) 

OR

->addAttributeToSort(array('created_at', 'news_from_date'), 'desc')) 

回答

1

SQL顺序子句中没有“OR”,因此您需要找到一种方法将问题简化为单个列。以下是未经测试,但应该给你一个想法。

// Make sure the correct attributes are available with names we choose. 
$products->addAttributeToSelect('created_at, news_from_date'); 

// Choose the LATEST date to sort as it is the most RECENT. 
$products->addOrder('MAX(created_at, news_from_date)', 'desc'); 
0

您可以订购/排序最新产品双方created_atnews_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'); 

感谢。

+0

什么左边是什么意思? – 2011-03-21 20:16:26

+0

'左'表示左加入。 addAttributeToFilter的最后一个参数是连接类型。 – 2011-03-22 04:27:45

0

我建议使用news_from_date而不是创建日期。这使您可以提前灵活地创建内容,但在发布内容时仍然按正确顺序显示。

0
更好

$_productCollection = $this->getLoadedProductCollection()->clear()->addAttributeToSort('created_at', 'DESC')->setPageSize(4);