get product collection

piaoling  2011-08-30 10:23:55

protected function getRandproduct() {
        $homeProductNumber = 9;
        $collection = $this->_getVisNoRandCol();
        $tempCollection = $this->_addProductAttributesAndPrices($collection)
                               ->addAttributeToSelect('*')
                               ->addFieldToFilter(array(array('attribute'=>'page_accueil','eq'=>'1')))
                               ->setPageSize($homeProductNumber)
                               ->setCurPage(1);
        if($tempCollection->getSize() <= 0){
            $collection = $this->_getVisCol();
            $tempCollection = $this->_addProductAttributesAndPrices($collection)
                                   ->addAttributeToSelect('*')
                                   ->setPageSize($homeProductNumber)
                                   ->setCurPage(1);
        }
        $collection = new Varien_Data_Collection();
        foreach ($tempCollection as $item){
            $product = Mage::getModel('catalog/product')->load($item->getEntityId());
            $collection->addItem($product);
        }
        return $collection;
    }

protected function _getVisCol(){
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->getSelect()->order('rand()');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
        return $collection;
    }
    protected function _getVisNoRandCol(){
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->getSelect()->order('e.entity_id ASC');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
        return $collection;
    }

类别 :  默认(751)  |  浏览(3311)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: