Magento定制产品系列分页不起作用

时间:2013-04-10 10:50:04

标签: magento collections pagination toolbar product

我在CMS页面中显示Bestseller产品列表,方法是使用XML Layout来渲染块。与分页一起,我需要像在其他产品列表中一样显示工具栏(分页和排序)。因此我为此创建了一个自定义模块。有了这个,我能够显示产品列表,并且工具栏也会出现。但排序(或任何功能)似乎不起作用。请帮帮我。我的代码如下。

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>

我无法弄清楚代码的问题。请帮帮我。

1 个答案:

答案 0 :(得分:1)

我的收藏品已经加载了类似的问题。这样可以防止寻呼机中的所有过滤器被应用。尝试将构造函数更改为

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

如果您再次加载畅销书集合,可以通过调用count来表示。然后尝试

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

希望这能帮到你需要的地方