0

我用函数在 app/code/local/myown/layoutmods/catalog/block/product/list/ 中编写了 Manufacturer.php 文件。现在,当我尝试使用它时,它会加载所有产品。我检查并调用了该文件,但它不会按制造商过滤。谢谢大家的建议。

protected function _getProductCollection() {
        if (is_null($this->_productCollection)) {

            // Get attribute value id:
            $manufacturer_name = Mage::registry( 'current_category' )->getName();
            $productModel = Mage::getModel('catalog/product');
            $attr = $productModel->getResource()->getAttribute('manufacturer');
            if ($attr->usesSource()) {
                $manufacturer_id = $attr->getSource()->getOptionId($manufacturer_name);
            }

            // load and filter product collection
            $collection = Mage::getModel('catalog/product')->getCollection();
            Mage::getModel('catalog/layer')->prepareProductCollection($collection);
            $collection->addStoreFilter();
            $this->_productCollection = $collection;
            $this->_productCollection->addAttributeToSelect( 'manufacturer' )->addAttributeToFilter( 'manufacturer', array( 'eq' => $manufacturer_id ) );
        } 

        return $this->_productCollection;
    }
4

1 回答 1

0

据我所知,问题不在产品集合或过滤器中,而是在 $manufacturer_id 参数中。

如果您注释掉 // Get attribute value id: 和 // load and filter product collection comments 之间的代码,然后将 $manufacturer_id 替换为数据库中的其中一个 id,它将起作用。我也不得不注释掉

Mage::getModel('catalog/layer')->prepareProductCollection($collection);

就我而言,这条线阻止了任何东西进入收藏,所以我不确定它到底做了什么。

我看到的第一个问题是 $manufacturer_id 不一定总是被设置,因为

if ($attr->usesSource()) {

我无法让代码运行,因为 Mage::registry( 'current_category' ) 在我测试它时不包含值。我建议您重新测试用于检索制造商 ID 的代码。

于 2013-01-31T09:12:00.110 回答