0

I have three categories like CAT-A, CAT-B, CAT-C and I want the product collection from these by the order CAT-A products, CAT-B products, CAT-C products.

When I trying to get the product collection without any filter, I get the random products from these categories.

I'm trying to filter the product collection by category like

$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSort('category_id', 'ASC')

but it's not working.

3
  • 1
    categoryid is not attribute so that is why its not working Sep 23, 2016 at 13:30
  • then, How can I achieve this? @Murtuza Sep 23, 2016 at 13:32
  • You Need to join with category for that find my answer for that Sep 23, 2016 at 13:33

2 Answers 2

0

You have to join the category_product table if you want to be able to sort them because the category isn't a product attribute.
I believe there is a addCategoryFilter function that you can add to your collection, which takes a Mage_Catalog_Model_Category as an argument.
But this might prove inefficient if you have 3 categories and you might have to resort to using a join.

0

The code below might help you with JOINing categories to your product collection

$_rootcatID ='yourId';
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
  ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
  ->addAttributeToSort('category_id', array('in' => $_rootcatID))
1
  • It's not working.. Actually, I want the product collection by the order CAT-A products, CAT-B products, CAT-C products Sep 23, 2016 at 13:45

Your Answer

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

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