特定类别布局更新,自定义产品集合 - 缺少工具栏?

时间:2012-11-23 19:35:38

标签: magento layout toolbar categories

在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(); ?>返回一个空字符串,但我不明白为什么。

任何帮助表示感谢。

干杯, 迈克尔。

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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();
?>

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

干杯, 迈克尔。

相关问题