Magento-对非对象调用成员函数getId()

Magento-对非对象调用成员函数getId(),magento,collections,categories,Magento,Collections,Categories,我有以下代码,其中显示了当前类别中的5种产品(具有cat ID:63): 这很好,但我想进一步调整它,以便它只显示在另一个类别(cat ID:71)中也可以找到的产品,使用$collection->addCategoryFilter(71)但当我执行此操作时,会出现错误: Fatal error: Call to a member function getId() on a non-object in 完整代码为: $_helper = $this->helper('catalog

我有以下代码,其中显示了当前类别中的5种产品(具有cat ID:63):

这很好,但我想进一步调整它,以便它只显示在另一个类别(cat ID:71)中也可以找到的产品,使用
$collection->addCategoryFilter(71)但当我执行此操作时,会出现错误:

Fatal error: Call to a member function getId() on a non-object in
完整代码为:

$_helper    = $this->helper('catalog/output');
$_category  = $this->getCurrentCategory();   
$collection = $_category->getProductCollection();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);

// this line throws the error
$collection->addCategoryFilter(71);

$numProducts = 5;
$collection->setPage(1, $numProducts)->load();


foreach($collection as $_product){
    // output products...
};
这条线不对

$collection->addCategoryFilter(71);
我猜您正在尝试筛选id=71的类别。正确的? 如果是这样,您可以这样做:

$catToFilter = Mage::getModel('catalog/category')->load(71);
$collection->addCategoryFilter($catToFilter);
出现问题的原因是,当类别筛选器方法需要对象时,您正在提供id。因此,您将获得id为71的category对象,并将其作为参数传递

==================================================================================

已更新:仅显示两个类别中常见的产品

// 1st category
$cat1 = Mage::getModel ('catalog/category')->load(63);

//2nd category
$cat2 = Mage::getModel ('catalog/category')->load(71);

//loading cat2 products
$collection = $cat2->getProductCollection();

//this keeps the common products and remaining products of cat1
$collection->addCategoryFilter($cat1);

$removeList = array();
foreach ($collection as $prod)
{
  $prodCatIds = $prod->getCategoryIds();
  //if the current collection products does not lie in cat2
  if (! in_array($cat2->getId(), $prodCatIds))
  {
    //creating a list of product ids that are not common 
    array_push($removeList , $prod->getId());
  }

}

//removing the UNCOMMON products from the collection
foreach($removeList as $rl)
  $collection->addAttributeToFilter('entity_id', array('neq' => $rl)) ;

//The collection is now ready
foreach($collection->getData() as $_product){
  //$_product details or load product object
  zend_debug::dump($_product['sku']);
}

有点脏,但希望有帮助。

同样,我建议您使用url键或名称或id以外的其他属性来加载类别。谢谢,这对我来说很有用,但我刚刚意识到,上面显示的是63或71类别的产品,我需要这两个类别的产品-无论如何,我可以这样做?好的,是的。与您最初想要的不同,检查更新的答案。
// 1st category
$cat1 = Mage::getModel ('catalog/category')->load(63);

//2nd category
$cat2 = Mage::getModel ('catalog/category')->load(71);

//loading cat2 products
$collection = $cat2->getProductCollection();

//this keeps the common products and remaining products of cat1
$collection->addCategoryFilter($cat1);

$removeList = array();
foreach ($collection as $prod)
{
  $prodCatIds = $prod->getCategoryIds();
  //if the current collection products does not lie in cat2
  if (! in_array($cat2->getId(), $prodCatIds))
  {
    //creating a list of product ids that are not common 
    array_push($removeList , $prod->getId());
  }

}

//removing the UNCOMMON products from the collection
foreach($removeList as $rl)
  $collection->addAttributeToFilter('entity_id', array('neq' => $rl)) ;

//The collection is now ready
foreach($collection->getData() as $_product){
  //$_product details or load product object
  zend_debug::dump($_product['sku']);
}