【问题标题】:How do I filter out products from certain categories from Magento search?如何从 Magento 搜索中过滤掉某些类别的产品?
【发布时间】:2013-09-04 06:41:23
【问题描述】:

我尝试在 Mage/Catalog/Block/Product/List.php 上添加以下内容,_getProductCollection()

 $this->_productCollection
->addAttributeToFilter('category_id', array('nin' => array('36,37'),));

但它想出了:

Fatal error: Call to a member function getBackend() on a non-object in C:\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816

我怀疑是因为它正在寻找产品没有“category_id”的属性,我尝试添加:

 $this->_productCollection
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('nin' => array('36,37'),));

但这带来了另一个错误:

Joined field with this alias is already declared"

我做错了什么?
Mage/Catalog/Block/Product/List.php 是应用此覆盖的正确文件吗?
我只是希望它适用于搜索结果,即catalogsearch/result/

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    尝试使用 ** ->addCategoriesFilter($category);** 而不是 ->addAttributeToFilter(

    它对我有用:)

    【讨论】:

    • 我试过了,但是 ->addCategoriesFilter($category) 只过滤一个类别,它添加一个类别,而不是删除。我需要删除几个类别。
    【解决方案2】:

    其实这里需要一点小改动

    ->addAttributeToFilter('category_ids',array('nin'=>'36'))

    category_ids 而不是 category_id ;)

    【讨论】:

    • Hi Sandeep 此更改带来了 SQL 错误:“where 子句”中的未知列“e.category_ids”
    • 使用->printLogQuery(true) 把sql粘贴到这里让我检查
    • 评论太大了。请看下面
    【解决方案3】:

    这是我使用 addAttributeToFilter('category_ids') 时导致错误的 SQL

    选择e.*, search_result.relevance, price_index.price, price_index.tax_class_id, price_index.final_price, IF(price_index.tier_price IS NOT NULL , LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS minimal_price, price_index.min_price, price_index.max_price, price_index.tier_price, @9876543337. 987654338@ AS cat_index_position FROM catalog_product_entity AS e INNER JOIN catalogsearch_result AS search_result ON search_result.product_id=e.entity_id AND search_result.query_id='2677' INNER JOIN catalog_product_index_price AS price_index on price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 INNER JOIN catalog_category_product_index AS cat_index ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.visibility IN(3, 4) AND cat_index.category_id='2' WHERE (e.@ 987654349@ NOT IN('36,37')) AND (e.category_ids NOT IN('36,37')) ORDER BY relevance desc LIMIT 20

    【讨论】:

      【解决方案4】:

      嗯,我得到了它的工作。试试这个:

      /*my rand products here*/
      $products->getSelect()->order(new Zend_Db_Expr('RAND()'));
      $products->setPageSize(4)->setCurPage(1);
      $this->setProductCollection($products);
      
      /*this made the trick*/
      $array_ids_cat=array();
      foreach($category_ids as $cat_id){
          $array_ids_cat[]=array('finset'=>$cat_id);
      }
      
      $_products = $this->getProductCollection()->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')                                            ->addAttributeToFilter('category_id', $array_ids_cat  );
      

      【讨论】:

        猜你喜欢
        • 2017-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多