2013-04-10 66 views
0

我在CMS頁面中使用XML佈局顯示Bestseller產品列表以顯示該塊。與分頁一起,我需要顯示其他產品列表中的工具欄(分頁和排序)。因此我爲此創建了一個自定義模塊。有了這個,我可以顯示產品列表和工具欄。但排序(或任何功能)似乎沒有工作。請幫助我。我的代碼如下。Magento自定義產品收集分頁不起作用

class MyCompany_Bestseller_Block_Bestseller extends Mage_Catalog_Block_Product_Abstract //Mage_Core_Block_Template 
{ 
    public function __construct() 
    { 
    parent::__construct(); 
     $collection = $this->getBestsellerProduct(); 
     $this->setCollection($collection); 
    } 

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

    $toolbar = $this->getToolbarBlock(); 

     // called prepare sortable parameters 
     $collection = $this->getCollection(); 

     // use sortable parameters 
     if ($orders = $this->getAvailableOrders()) { 
      $toolbar->setAvailableOrders($orders); 
     } 
     if ($sort = $this->getSortBy()) { 
      $toolbar->setDefaultOrder($sort); 
     } 
     if ($dir = $this->getDefaultDirection()) { 
      $toolbar->setDefaultDirection($dir); 
     } 
     if ($modes = $this->getModes()) { 
      $toolbar->setModes($modes); 
     } 

     // set collection to toolbar and apply sort 
     $toolbar->setCollection($collection); 

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

     $this->getCollection()->load(); 
     return $this; 
    } 

    public function getToolbarBlock() 
    { 
     $block = $this->getLayout()->createBlock('bestseller/toolbar', microtime()); 
     return $block; 
    } 
    public function getMode() 
    { 
     return $this->getChild('toolbar')->getCurrentMode(); 
    } 

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

    function getBestsellerProduct() 
    { 

    // store ID 
    $storeId = Mage::app()->getStore()->getId();  

    // get most ordered products 
    $products = Mage::getResourceModel('reports/product_collection') 
        ->addAttributeToSelect('*')  
        ->addOrderedQty() 
        ->setStoreId($storeId) 
        ->addStoreFilter($storeId)      
        ->setOrder('ordered_qty', 'desc');          

    Mage::getSingleton('catalog/product_status') 
      ->addVisibleFilterToCollection($products); 
    Mage::getSingleton('catalog/product_visibility') 
      ->addVisibleInCatalogFilterToCollection($products); 

    return $products; 
    } 
} 

爲我的模塊Toolbar.php塊代碼如下:

class MyCompany_Bestseller_Block_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar 
{ 
    public function getPagerHtml() 
    { 
     $pagerBlock = $this->getLayout()->createBlock('page/html_pager'); 

     if ($pagerBlock instanceof Varien_Object) { 

      /* @var $pagerBlock Mage_Page_Block_Html_Pager */ 
      $pagerBlock->setAvailableLimit($this->getAvailableLimit()); 

      $pagerBlock->setUseContainer(false) 
      ->setShowPerPage(false) 
      ->setShowAmounts(false) 
      ->setLimitVarName($this->getLimitVarName()) 
      ->setPageVarName($this->getPageVarName()) 
      ->setLimit($this->getLimit()) 
      ->setCollection($this->getCollection()); 
      return $pagerBlock->toHtml(); 
     } 
     return ''; 
    } 
} 

這裏是我已經使用的XML塊。

<reference name="content"> 
      <block type="bestseller/bestseller" name="bestseller_list" template="bestseller/bestseller.phtml"> 
       <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml"> 
        <block type="page/html_pager" name="product_list_toolbar_pager"/> 
        <!-- The following code shows how to set your own pager increments --> 
        <!-- 
         <action method="setDefaultListPerPage"><limit>4</limit></action> 
         <action method="setDefaultGridPerPage"><limit>9</limit></action> 
         <action method="addPagerLimit"><mode>list</mode><limit>2</limit></action> 
         <action method="addPagerLimit"><mode>list</mode><limit>4</limit></action> 
         <action method="addPagerLimit"><mode>list</mode><limit>6</limit></action> 
         <action method="addPagerLimit"><mode>list</mode><limit>8</limit></action> 
         <action method="addPagerLimit" translate="label"><mode>list</mode><limit>all</limit><label>All</label></action> 
        --> 
       </block> 
       <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action> 
       <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action> 
       <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action> 
       <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action> 
       <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action> 
       <action method="setToolbarBlockName"><name>product_list_toolbar</name></action> 
      </block> 
     </reference> 

我無法弄清楚代碼的問題。請幫助我。

+0

也顯示XML代碼塊? – 2013-04-10 11:58:43

+0

我現在也添加了該塊的xml代碼。 – Rujen 2013-04-11 04:56:04

回答

1

我有一個類似的問題,我的集合已被加載。這可以防止應用分頁器中的所有過濾器。嘗試更改您的構造函數爲

public function __construct() 
{ 
    parent::__construct(); 
    $collection = $this->getBestsellerProduct()->clear(); 
    $this->setCollection($collection); 
} 

如果您要再次通過調用計數來加載暢銷書集合。然後嘗試

public function __construct() 
{ 
    parent::__construct(); 
    $collection = clone $this->getBestsellerProduct(); 
    $this->setCollection($collection->clear()); 
} 

希望這可以讓你在那裏你需要

相關問題