I am trying to display only those products which are present in both category 1 and category 2 or in other words only those products which are present in both parent category and its sub category I tried this:
$collection = Mage::getModel('catalog/product') ->getCollection() ->distinct(true) ->addAttributeToSelect('*') ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left') ->addAttributeToFilter('category_id',$parentcategory) ->addAttributeToFilter('category_id',$subcategory ); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
foreach($collection as $product){ echo $product->getId(); }
EDIT:I used this to get product id but its giving error of getId on non-object