0

我正在编写一个为我加载特色产品列表的模块。所有特色产品都位于自己的类别 + 隐藏类别“特色”中。该脚本返回一个错误。

在类别视图(list.phtml)上,我要求gettopproducts.phtml(效果很好):

<?php $currentCategory = Mage::registry('current_category'); ?>
<?php $_products = $this->getTopProducts($currentCategory); ?>
<?php echo $this->__('Available products: ').$_products->count(); ?>

gettopproducts.phtml我调用一个传递当前类别的函数getTopProducts()Gettopproducts.phpGettopproducts.php我有这个:

public function getTopProducts($currentCategory)
{
    $_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addCategoryFilter($currentCategory)
    ->addAttributeToFilter('category_ids',array('finset'=>'87'));
    $_productCollection->load();
    return $_productCollection;
}

此行:->addAttributeToFilter('category_ids',array('finset'=>'87'));应添加第二个类别过滤器(“特色”类别的 ID)。但是当我使用它时,我得到一个错误。当我删除这一行时:->addAttributeToFilter('category_ids',array('finset'=>'87'));它工作得很好。

我正在使用 Magento 1.7.2

4

2 回答 2

1

我找到了原因。对于 Magento 1.4+,类别 ID 不是报告/产品集合的成员。

这是如何获得它的方式:替换->addAttributeToFilter('category_ids',array('finset'=>'87')); 为:

$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87'))); 

所以代码看起来像这样:

$_productCollection = Mage::getResourceModel('reports/product_collection');
$_productCollection->addAttributeToSelect('*');
$_productCollection->addCategoryFilter($currentCategory);
$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87')));        
$_productCollection->load();
return $_productCollection;
于 2013-02-14T14:47:35.230 回答
0

对于 magento 1.7,这对我有用:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('id')        
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('home_slider', array('neq' => ''))
    ->addAttributeToFilter('home_slider_value', $slide_num)
    ->addStoreFilter();
    //here pass an array() of CATEGORY IDs
    $catids = array('id_1,id_2, etc..'); 

    $statements = array();
    foreach ($catids as $categoryId){
        if (is_numeric($categoryId)){
         $statements[] = "{{table}}.category_id = $categoryId";
        }
    }

    $collection->distinct(true)
    ->joinField('category_id','catalog/category_product', null,'product_id = entity_id', implode(" OR ",$statements),'inner');
于 2016-02-18T14:20:42.733 回答