0

I am having problem apply filters based on categories. The situation is I load all products belonging to a category and and then based on few parameter apply further category filtering like the query might be to fetch all products belonging to "flags" and "skulls" category from all products in clothing category. (category == "clothing" && (category == "flags" || category == "skulls"))

1. $collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection(); //load all products in category with id $categoryID

2. $collection->addCategoryFilter(Mage::getModel('catalog/category')->load(array(implode(',', $category_filters_ids))), true);

The problem with line no 2 is that it brings out all categories belonging to ids given in $category_filter_ids without enforcing $categoryId from line 1.

3. $collection->addAttributeToFilter('category_ids',array('in'=>$category_filters_ids));

this gives "Call to a member function getBackend() on a non-object" error. My goal is to load only products belonging to id $category and then further those which belong any of the ids in $category_filter_id array. Please help me

0

You can try this.

<?php
$category_filters_ids = array(1, 2, 3);
array_push($category_filters_ids, $categoryId);
array_unique($category_filters_ids);

$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' => $category_filters_ids));
$collection->getSelect()->group('e.entity_id');
?>

If that doesn't work try below

<?php
$category_filters_ids = array(1, 2, 3);
array_push($category_filters_ids, $categoryId);
array_unique($category_filters_ids);

$collection = mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*');

$conditions = array();
foreach ($category_filters_ids as $categoryId) {
 if (is_numeric($categoryId)) {
  $conditions[] = "{{table}}.category_id = $categoryId";
 }
}

$collection->distinct(true)
->joinField('category_id', 'catalog/category_product', null, 'product_id = entity_id', implode(" OR ", $conditions), 'inner');

?>
| improve this answer | |
  • Thanks @Akhilesh Patel for your help. Above solution worked :) – Danish Altaf Satti Oct 5 '15 at 11:41

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.