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

时间:2013-09-04 06:41:23

标签: magento magento-1.7

我尝试在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 /

4 个答案:

答案 0 :(得分:0)

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

它对我有用:)

答案 1 :(得分:0)

实际上这里需要进行一些小改动

- > addAttributeToFilter( 'category_ids',阵列( 'nin的'=> '36' ))

category_ids而不是category_id;)

答案 2 :(得分:0)

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

SELECT e。*,search_resultrelevanceprice_indexpriceprice_indextax_class_id,{{ 1}}。price_index,IF(price_index.tier_price IS NOT NULL,LEAST(price_index.min_price,price_index.tier_price),price_index.min_price)AS final_priceminimal_price。{{1 },price_indexmin_priceprice_indexmax_priceprice_indextier_price AS cat_index FROM position AS cat_index_position  INNER JOIN catalog_product_entity AS e ON search_result.product_id = e.entity_id AND search_result.query_id ='2677'  INNER JOIN catalogsearch_result AS search_result ON price_index.entity_id = e.entity_id AND price_index.website_id ='1'AND price_index.customer_group_id = 0  INNER JOIN catalog_product_index_price AS price_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({ {1}}。catalog_category_product_index NOT IN('36,37'))和(cat_indexe NOT IN('36,37'))按category_ids排序desc LIMIT 20

答案 3 :(得分:0)

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

/*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  );