0

在 local.xml 中,我创建了一个布局更新,以便我可以显示自定义过滤的产品集合。

这是在 local.xml 中:

<CATEGORY_7>
        <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
            <block type="catalog/product_list" name="product_list" template="catalog/product/cashcrop.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"/>
                </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>
                <action method="setColumnCount"><columns>4</columns></action>

            </block>
        </block>
</CATEGORY_7>

模板文件是 list.phtml 的副本,但经过修改以过滤集合:

<?php
    $_productCollection = Mage::getModel('catalog/product')->getCollection();
    //$_productCollection=$this->getLoadedProductCollection();
    $_productCollection
    ->addAttributeToSelect('*')
    //->addAttributeToFilter('status', 1)
    //->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('randament', array('in' => array(101, 102)))
    ->load()
    ;
    $this->setCollection($_productCollection);
    $_helper = $this->helper('catalog/output');
?>

这行得通,我在集合中获得了 105 种产品。问题是工具栏 - 它没有被显示。有谁知道为什么工具栏没有显示?(我知道这<?php echo $this->getToolbarHtml(); ?>会返回一个空字符串,但我不明白为什么。

任何帮助表示赞赏。

干杯,迈克尔。

4

2 回答 2

0

其实,我自己想通了。覆盖模板没问题,放在类别的自定义布局更新部分就足够了;无需将其放在 local.xml 中。

问题在于 Mage/Catalog/Block/Product/List.php 中的 _beforeHtml() 函数,其中工具栏由块的 _getProductCollection() 函数初始化,该函数总是返回一个空集合,因为它试图获取当前类别的产品集合。

因此,作为一个快速而肮脏的修复,我只是从 _beforeHtml() 函数中复制了代码,并将其直接插入到我的 cashcrop.phtml 模板中。模板的顶部现在看起来像这样:

<?php
    $_productCollection = Mage::getModel('catalog/product')->getCollection();
    $_productCollection->addAttributeToSelect('*')
                        ->addAttributeToFilter('randament', array('in' => array(101, 102)))
                        ->addAttributeToFilter('inflorire', array('in' => array(97)))
                        ->addMinimalPrice()
                        ->addFinalPrice()
                        ->addTaxPercents();

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

    $_helper = $this->helper('catalog/output');

    $toolbar = $this->getToolbarBlock();
    $collection = $_productCollection;

    // 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($_productCollection);

    $this->setChild('toolbar', $toolbar);
    Mage::dispatchEvent('catalog_block_product_list_collection', array(
        'collection' => $_productCollection
    ));

    $_productCollection->load();
?>

我知道这可能不是理想的解决方案,但它正在工作。如果其他人有更好的解决方案,我很想听听。

干杯,迈克尔。

于 2012-11-25T11:40:56.990 回答
0

您不需要重新初始化所有内容。试试这个,看看它是否有效:

<CATEGORY_7>
    <reference name="product_list">
        <action method="setTemplate">
            <tpl>catalog/product/cashcrop.phtml</tpl>
        </action>
    </reference>
</CATEGORY_7>

另外,我会警惕在脚本或布局文件中使用自动增量数据。我倾向于将它放在数据库中类别的布局更新中。可能与您的环境无关,但 7 是存储后端的任意功能,而不是更有意义的东西,例如类别名称。

于 2012-11-23T20:05:56.637 回答