Magento 中基于属性的产品列表

发布于 2024-12-15 03:54:45 字数 394 浏览 1 评论 0原文

我想在 Magento 的产品列表中显示“建议”。我创建了一个属性“建议”,它是/否且全局有效。现在,在列表中,我想首先显示建议,然后显示一些文本和内容,然后显示其余产品。

我这样尝试:

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

但这以异常结束:

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

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

I would like to show "Suggestions" in my product listing in Magento. I made an attribute "Suggestion" which is Yes/No and global active. Now in the listing I would like to show the suggestions first, then some text and stuff, and then the rest of products.

I tried it like this:

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

But this ends in an exception:

You cannot define a correlation name '_price_rule' more than once

Now the question is, how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2024-12-22 03:54:45

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

/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 中加载此方法并且它起作用了:)
无论如何,感谢您的阅读!也许这段代码可以帮助某人!

Ok, the solution for me was a custom module, which extends the List Functionality. I added the following file:

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

With the following extending code:

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;
}

Then I loaded this method in the List.phtml and it worked :)
Thanks for reading anyway! Maybe this code helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文