0

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

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>

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

4

1 回答 1

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());
}

希望这能让你到达你需要的地方

于 2013-04-24T21:15:17.407 回答