Magento限制主页中的产品数量

时间:2011-07-20 10:19:12

标签: magento

我已在cms主页

中添加了此代码{{block type="catalog/product_list" category_id="25" template="catalog/product/list.phtml"}}

我想限制不向这个类别显示九个产品。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

我认为没有值可以传递给块标记来限制它。我建议制作一个新的list.phtml文件,限制它。

让我快速看一下代码。

确定。如果您要复制文件/app/design/frontend/default/default/template/catalog/product/list.phtml

/app/design/frontend/default/default/template/catalog/product/list-limit.phtml

然后按如下方式对其进行编辑:

LINE49: After the foreach
<?php if($_iterator >=9) { break; } ?>
LINE94: Where $_collectionSize is assigned change to: 
<?php $_collectionSize = main(9, $_productCollection->count()) ?>
Line97: After the foreach 
<?php if($i >= 9) { break; } ?>

无论Grid或List视图如何,它都应该达到您的目的。

......很快就会有另一种方法......

另一种方法是编辑加载phtml文件所呈现的产品列表的List.php文件。块类型'catalog / product_list'表示您需要文件:

/app/code/core/Mage/Catalog/Block/Product/List.php

在那里你会看到方法getLoadedProductCollection,它调用_getProductCollection。可以编辑该代码以过滤/限制返回的产品数量。您可能希望复制该文件,并更新页面中的阻止链接。不要在名称中添加下划线,因为这需要将文件放在子目录中。

希望这有帮助。

答案 1 :(得分:1)

继上一个回答之后,我似乎通过在第96行之后添加以下内容来编辑List.php来实现这一点。

return $this->_productCollection

            ->setPageSize($this->getProductsCount());

    }

    /**
     * Set how much product should be displayed at once.
     *
     * @param $count
     * @return Mage_Catalog_Block_Product_New
     */
    public function setProductsCount($count)
    {
        $this->_productsCount = $count;
        return $this;
    }

    /**
     * Get how much products should be displayed at once.
     *
     * @return int
     */
    public function getProductsCount()
    {
        if (null === $this->_productsCount) {
            $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
        }
        return $this->_productsCount;
    }

并在第43行之后添加此内容

/**
 * Default value for products count that will be shown
 */
const DEFAULT_PRODUCTS_COUNT = 100;

/**
 * Products count
 *
 * @var null
 */
protected $_productsCount;

我从new.php获得了代码

相关问题