dongxing8766 2011-11-10 09:54
浏览 40
已采纳

基于Magento属性的ProductListing

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?

  • 写回答

1条回答 默认 最新

  • dtjpz48440 2011-11-22 09:48
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用