Magento如何首先按新产品排序

时间:2014-03-10 07:09:55

标签: magento

我尝试使用下面的代码,但这段代码只提供新产品,我需要新产品冷杉,然后是其他产品。

$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'); 

任何一个想法?

提前致谢!!!

3 个答案:

答案 0 :(得分:2)

安装此扩展程序 sort by date

请参阅以下步骤在图像中定义

enter image description here

答案 1 :(得分:1)

请试试这个

$collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter(array(array('attribute'=>'status', 'eq'=>'1')))
            ->addAttributeToSort('created_at', 'DESC');

答案 2 :(得分:1)

最后我得到了我的一个问题答案:)

==================在/Catalog/Product/List/Toolbar.php文件中设置此代码============= ====

private function _getNewProducts() {
    $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    $collectionNew = 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');
    return $collectionNew->getAllIds();
}

public function setCollection($collection) {
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int) $this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }
    if ($this->getCurrentOrder() == 'newest') {
        $this->_collection->getSelect()->order('FIElD(e.entity_id, ' . implode(',', $this->_getNewProducts()) .') DESC ');
    }

    return $this;
}


public function getAvailableOrders() {

    $this->_availableOrder = array(
        'newest' => $this->__('New Product'),
        'name' => $this->__('Name'),
        'price' => $this->__('Price')
    );
    return $this->_availableOrder;
}

这项工作完美!! :)