0

我正在尝试添加位于此处的 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.

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

任何帮助将不胜感激。

4

1 回答 1

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');
    }
}
于 2013-05-08T16:28:07.537 回答