2013-05-08 28 views
0

我想补充Mostviewed位于:Magento的 - 如何在Magento与分页创建页1.7

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.

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

任何帮助将不胜感激。

回答

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'); 
    } 
} 
+0

我想你的代码,它的工作well.I想显示下拉页面像5 10 15 20 ...,怎么改变数量。另外一个,如何添加排序? – 2013-05-09 08:53:24

+0

我编辑了我的答案。 – ndlinh 2013-05-09 09:51:22

+0

我用第二method.I排序cliked通过但不change.I认为有什么问题'公共职能__construct()'。可你能帮我吗? – 2013-05-11 03:43:02