magento - 如何在magento 1.7中创建具有分页的页面

时间:2013-05-08 04:22:33

标签: magento magento-1.7

我正在尝试添加位于此处的Mostviewed:

App/code/local/Mage/Catalog/Block/Product/Mostviewed.php

我添加了以下代码:

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
   public function __construct(){
        parent::__construct();
        $storeId = Mage::app()->getStore()->getId();
        $collection = Mage::getResourceModel('reports/product_collection')
        ->addViewsCount();
        $collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
        $this->setProductCollection($collection);
        return parent::_beforeToHtml();
    }
}

我已添加echo $this->getToolbarHtml()位于此处:

app/design/frontend/default/default/template/catalog/product/mostviewedlist.phtml

无法在mostviewedlist.phtml.

中显示分页

有人可以帮我解决这个问题吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你的座位应该是这样的:

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
    public function __construct(){
        parent::__construct();
        $storeId = Mage::app()->getStore()->getId();
        $collection = Mage::getResourceModel('reports/product_collection')
            ->addViewsCount();
        $collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
        $this->setProductCollection($collection);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $pager = $this->getLayout()->createBlock('page/html_pager', 'mostview.pager')
                      ->setCollection($this->getProductCollection());
        $this->setChild('pager', $pager);
        $this->getProductCollection()->load();

        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
}

您的模板应该是这样的:

<?php $_collection = $this->getProductCollection(); ?>

<!-- top pagination -->
<?php echo $this->getPagerHtml(); ?>

<?php if($_collection->getSize()): ?>
    ...
    <?php foreach ($_collection as $_item): ?>
       ...
    <?php endforeach; ?>
<?php endif ?>

<!-- bottom pagination -->
<?php echo $this->getPagerHtml(); ?>

如果要显示工具栏。你的块应该是这样的: (注意:我没有测试此代码)

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
    public function __construct(){
        parent::__construct();
        $storeId = Mage::app()->getStore()->getId();
        $collection = Mage::getResourceModel('reports/product_collection')
            ->addViewsCount();
        $collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
        $this->setProductCollection($collection);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $toolbar = $this->getLayout()->createBlock('catalog/product_list_toolbar', microtime())
            ->setCollection($this->getProductCollection());

        $pager = $this->getLayout()->createBlock('page/html_pager', microtime());
        $toolbar->setChild('product_list_toolbar_pager', $pager);

        $this->setChild('toolbar', $toolbar);
        $this->getProductCollection()->load();

        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('toolbar');
    }
}