Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

filter products by 2 categories
 
Loraros
Jr. Member
 
Total Posts:  5
Joined:  2013-02-05
 

Hi,

sorry for some bad english grin but i hope you can help me with a problem.

i am searching for a way how to filter products in Magento using 2 categories.
I’ve read many many thread but none of their solutions worked for me.

my category tree looks like this:

- A
—A1
---- A1_1
---- A1_2
---- A1_3
—A2
---- A2_1
---- A2_2
---- A2_3
- B
—B1
---- B1_1
---- B1_2
---- B1_3
—B2
---- B2_1
---- B2_2
---- B2_3

now i want to display only products which are in both categories (e. g. cat A1_1 & A2_1).
Is there an easy way to do this?

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
sunil_nextbits
Jr. Member
 
Total Posts:  11
Joined:  2011-11-24
 

Hello,

Try below code:
$categories = array(7,45,233);

$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’ => $categories));
$collection->getSelect()->group(’e.entity_id’);

Thanks,
Sunil Patel

 
Magento Community Magento Community
Magento Community
Magento Community
 
Loraros
Jr. Member
 
Total Posts:  5
Joined:  2013-02-05
 

Thanks, thats close to what i want.

But now it shows me all products of category A and all products of category B together. I want it to show me only the products which are in both categories.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top