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

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

2 Answers 2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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