基于Magento属性的ProductListing

时间:2011-11-10 09:54:18

标签: php magento

我想在Magento的产品详情中显示“建议”。我创建了一个属性“Suggestion”,它是Yes / No和全局活动。现在在列表中我想首先显示建议,然后是一些文本和内容,然后是其他产品。

我试过这样:

$_productCollection=$this->getLoadedProductCollection()
/* .... */
$_productCollection->clear()->addAttributeToFilter('suggestion', 1)->load();

但这以一个例外结束:

  

您不能多次定义相关名称“_price_rule”

现在的问题是,如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,我的解决方案是一个自定义模块,它扩展了List功能。我添加了以下文件:

  

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

使用以下扩展代码:

protected function _getProductCollectionSuggestion()
{
    $layer = Mage::getSingleton('catalog/layer');
    /* @var $layer Mage_Catalog_Model_Layer */
    if ($this->getShowRootCategory()) {
        $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
    }

    // if this is a product view page
    if (Mage::registry('product')) {
        // get collection of categories this product is associated with
        $categories = Mage::registry('product')->getCategoryCollection()
            ->setPage(1, 1)
            ->load();
        // if the product is associated with any category
        if ($categories->count()) {
            // show products from this category
            $this->setCategoryId(current($categories->getIterator()));
        }
    }

    $origCategory = null;
    if ($this->getCategoryId()) {
        $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
        if ($category->getId()) {
            $origCategory = $layer->getCurrentCategory();
            $layer->setCurrentCategory($category);
        }
    }
    $this->_productCollection = $layer->getProductCollection();
    $this->_productCollection->addAttributeToFilter('suggestion', 1);
    if($this->_productCollection->count()) {
        foreach($this->_productCollection as $_productKey => $_product) {
            if($_product->getSuggestion() == 0 || !$_product->getSuggestion()) {

            }
        }
    }

    $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

    if ($origCategory) {
        $layer->setCurrentCategory($origCategory);
    }
    return $this->_productCollection;
}

然后我在List.phtml中加载了这个方法,它工作了:) 无论如何,感谢阅读!也许这段代码可以帮助别人!