0

i am looking for product collection with specific category filter. Lets say i have products in categories:


Category A: Prod_1, Prod_2, Prod_3

Category B: Prod_4, Prod_5, Prod_6

Category C: Prod_7, Prod_8, Prod_9

Category D: Prod_1, Prod_5, Prod_9

and i want to get products form Category D, but with condition that this products is also in Category A , so it must return:

Prod_1 only

0

Same as @Douglas except using "in" is faster. I'd comment but I don't have enough rep.

$catAId = 1; //  category a's id
$catBId = 4; // category d's id

$productsInAandD = Mage::getModel("catalog/product")
    ->getCollection()
    ->addAttributeToSelect('*')// not optimal
    ->joinField('category_id',
                'catalog/category_product',
                'category_id',
                'product_id=entity_id',
                 null,
                'left')
    ->addAttributeToFilter("category_id" , array('in' => array($catAId , $catBId)));

Also if your only going to read attribute values and not save the product it's faster to actually specify what attributes to select.

I.e

$collection->addAttributeToSelect(array('attribute_name_1','attribute_name_3'));
0

I've renamed your categories 1,2,3,4.

$_productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', array(
        array('finset' => '1'),
        array('finset' => '4'))
    )
    ->addAttributeToSort('created_at', 'desc');

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.