在Magento中获得至少一个类别的产品?

时间:2013-08-24 02:34:31

标签: magento

我目前有类别的产品和不属于任何类别的产品。这段代码将为我提供Magento中最近添加的所有产品:

$_productCollection = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter($preorderAttribute, array(
                    'eq' => Mage::getResourceModel('catalog/product')
                        ->getAttribute($preorderAttribute)
                        ->getSource()
                        ->getOptionId($preorderValue)
                ))
                ->setVisibility(array(2,3,4))
                ->setOrder('created_at', 'desc')
                ->setPage(1, 12);

但它也提供不属于任何类别的产品。我只想要一个或多个类别的产品,但不是任何类别的产品。

如何使用上述代码执行此操作?

1 个答案:

答案 0 :(得分:0)

下面的代码适用于我只显示特定的两类产品。希望它适用于你。

 $_productCollection = Mage::getModel('catalog/product')
                ->getCollection()
                ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('category_id', array(
                        array('finset' => '7'),
                        array('finset' => '8'))
                )
                ->addAttributeToSort('created_at', 'desc');
            foreach($_productCollection as $_testproduct){
                echo $_testproduct->getId()."<br/>";

            };
相关问题