0

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

  • Assume it's not returning the product you want? – Robbie Averill Dec 28 '15 at 18:43
  • no it's returning me error saying getId() on non-object in mage/../product/collection.php – Akash Dec 28 '15 at 18:50
  • There you go. It's a collection of products. You should loop the collection and call that attribute within the loop – Robbie Averill Dec 28 '15 at 18:57
1

Well it can be achieved by intersecting the collection result arrays.

As this is not straightforward and default Magento doesn't provide any facility to intersect collections, we have to do it manually as below:

$_productCollection1 = Mage::getModel('catalog/product')
 ->getCollection()
 ->distinct(true)
 ->addAttributeToSelect('entity_id')
 ->joinField('category_id', 'catalog/category_product', 'category_id', 
    'product_id = entity_id', null, 'left')
 ->addAttributeToFilter('category_id', array('eq' => 7))
 ->addAttributeToSort('created_at', 'desc');

 foreach($_productCollection1 as $product)
    $arr1[] = $product->getId();

 $_productCollection2 = Mage::getModel('catalog/product')
 ->getCollection()
 ->distinct(true)
 ->addAttributeToSelect('entity_id')
 ->joinField('category_id', 'catalog/category_product', 'category_id', 
    'product_id = entity_id', null, 'left')
 ->addAttributeToFilter('category_id', array('eq' => 8))
 ->addAttributeToSort('created_at', 'desc');

 foreach($_productCollection2 as $product)
    $arr2[] = $product->getId();

 $result = array_intersect($arr1, $arr2);
 foreach($result as $product_id):
    $productLoaded = Mage::getModel('catalog/product')->load($product_id);
    //do something with $productLoaded
endforeach;

Now you will have only products present in both categories 7 and 8.

Hope this will help.

|improve this answer|||||
  • this code is causing my page to loading and loading.. – Akash Dec 28 '15 at 19:21
  • above code is working only if I am passing only one category but not working when passing two – Akash Dec 28 '15 at 19:30
  • Updated my complete answer, please check and let me know. – Vicky Dev Dec 28 '15 at 19:36
  • still infinite loop i have visited ur link and already tried all possiblities from there – Akash Dec 28 '15 at 20:01
  • Updated my answer again, this is the last resort. It is working in my local system. – Vicky Dev Dec 28 '15 at 20:23
0

The answer should be:

$collection = Mage::getModel("catalog/product")
    ->getCollection()
    ->addAttributeToSelect('*')
    ->joinField(
        'category_id',
        'catalog/category_product',
        'category_id',
        'product_id=entity_id',
        null,
        'left'
    )
    ->addAttributeToFilter('category_id', array('in' => array(
        $id1,
        $id2
    )));
$collection->getSelect()->having('count(entity_id) > 1')->group('entity_id');
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.