如何从 Magento 搜索中过滤掉某些类别的产品?

Posted

技术标签:

【中文标题】如何从 Magento 搜索中过滤掉某些类别的产品?【英文标题】:How do I filter out products from certain categories from Magento search? 【发布时间】: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/

【问题讨论】:

【参考方案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  );

【讨论】:

以上是关于如何从 Magento 搜索中过滤掉某些类别的产品?的主要内容,如果未能解决你的问题,请参考以下文章

如何使magento过滤器像类别一样工作

Magento 类别页面未包含属性过滤器中的所有产品

无法在 magento 2.4.3 中查看类别下的产品

magento 从类别中获取产品,按 rand() 排序

从 list.phtml 结果中排除产品(magento)

Magento 购物车价格规则小计不由某些类别组成