3

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

4 回答 4

0

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

这个对我有用 :)

于 2013-09-04T06:58:19.827 回答
0

实际上这里需要做一个小改动

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

category_ids 而不是 category_id ;)

于 2013-09-06T04:18:59.017 回答
0

这是我使用 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, cat_index. 从positionAS内部 加入AS ON search_result.product_id=e.entity_id AND search_result.query_id='2677' INNER JOIN AS ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 INNER JOIN AScat_index_positioncatalog_product_entityecatalogsearch_resultsearch_resultcatalog_product_index_priceprice_indexcatalog_category_product_indexcat_indexON 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. category_idsNOT IN('36,37')) AND ( e. category_idsNOT IN('36,37')) 按relevancedesc LIMIT 20 排序

于 2013-09-09T23:42:14.403 回答
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  );
于 2015-08-13T09:39:23.117 回答