Magento - Load Product Collection with Pagination

為{幸葍}努か 提交于 2019-12-22 17:17:32

问题


I've been trying to load a product collection and then filter it by calling in the review ids into array and then applying that filter to it.

I've enclosed the code below which is in the top of the List.phtml that I'm running it through a custom copy of the list.phtml like so

<block type="catalog/product_list" name="sale" template="reviewsList/index.phtml">

Good news is that the collection will load, but it breaks the pagination. If anyone has any ideas that would be great.

Full code below.

Any help much appreciated.

<?php
$reviewCollection = Mage::getModel('review/review')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId())->addRateVotes()->setDateOrder();
$reviewArray = array();
foreach ($reviewCollection->getItems() as $thisReview):         
    array_push($reviewArray, $thisReview->getEntityPkValue());
endforeach;

$_productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => $reviewArray))->addAttributeToSelect('*')->setPageSize(5);

$_productCollection = $_productCollection->load();

//$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>

回答1:


To show pagination you can add a toolbar in list view. I have done that here for brands, a collection on category basis. You can modify this code to suit your collection.

class Mage_Catalog_Block_Product_Brandsnew extends Mage_Catalog_Block_Product_Abstract
{
protected $_productsCount = null;

const DEFAULT_PRODUCTS_COUNT = 5;

/**
 * Initialize block's cache
 */
protected function _construct()
{
    parent::_construct();

    $this->addColumnCountLayoutDepend('empty', 6)
        ->addColumnCountLayoutDepend('one_column', 5)
        ->addColumnCountLayoutDepend('two_columns_left', 4)
        ->addColumnCountLayoutDepend('two_columns_right', 4)
        ->addColumnCountLayoutDepend('three_columns', 3);

    $this->addData(array(
        'cache_lifetime'    => 86400,
        'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG),
    ));
}

/**
 * Get Key pieces for caching block content
 *
 * @return array
 */
public function getCacheKeyInfo()
{
    return array(
       'CATALOG_PRODUCT_NEW',
       Mage::app()->getStore()->getId(),
       Mage::getDesign()->getPackageName(),
       Mage::getDesign()->getTheme('template'),
       Mage::getSingleton('customer/session')->getCustomerGroupId(),
       'template' => $this->getTemplate(),
       $this->getProductsCount()
    );
}

/**
 * Prepare collection with new products and applied page limits.
 *
 * return Mage_Catalog_Block_Product_New
 */
protected function _beforeToHtml()
{
$toolbar = $this->getToolbarBlock();
//$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
   $collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addStoreFilter()
       ->addAttributeToFilter('manufacturer',$this->getRequest()->manufacturer);
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);

    //$collection->addAttributeToFilter('special_price' ,array('neq' => ''));

    // use sortable parameters
    if ($orders = $this->getAvailableOrders()) {
        $toolbar->setAvailableOrders($orders);
    }
    if ($sort = $this->getSortBy()) {
        $toolbar->setDefaultOrder($sort);
    }
        if (isset($_GET['p'])) {

        $toolbar->setLimit($toolbar->getLimit());
    }

    $this->setProductCollection($collection);

    $toolbar->setCollection($collection);

    $this->setChild('toolbar', $toolbar);
    Mage::dispatchEvent('catalog_block_product_list_collection', array(
        'collection'=>$collection,
    ));

    $collection->load();  
    return parent::_beforeToHtml();


}

/**
 * Set how much product should be displayed at once.
 *
 * @param $count
 * @return Mage_Catalog_Block_Product_New
 */
public function setProductsCount($count)
{
    $this->_productsCount = $count;
    return $this;
}

/**
 * Get how much products should be displayed at once.
 *
 * @return int
 */
public function getProductsCount()
{
    if (null === $this->_productsCount) {
        $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
    }
    return $this->_productsCount;
}


/**
 * Retrieve Toolbar block
 *
 * @return Mage_Catalog_Block_Product_List_Toolbar
 */
public function getToolbarBlock()
{
    if ($blockName = $this->getToolbarBlockName()) {
        if ($block = $this->getLayout()->getBlock($blockName)) {
            return $block;
        }
    }
    $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
    return $block;
}

public function setCollection($collection)
{
    $this->_productCollection = $collection;
    return $this;
} 

}


来源:https://stackoverflow.com/questions/7905830/magento-load-product-collection-with-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!