I would like to loop through a set of categories which are under a separate root, in order to generate a simple list of products. I put them under a separate root category because otherwise they end up appearing in odd places in the shop (they would be catalog visible)

This has been a nightmare as all the normal objects and collections seem to have a test that is about the site's own root (or something). At first I thought it was because of flat tables but it wasn't. It is the product collection's code.

I managed to find the category IDs via the tree:

$categoryid = 14;
$nodes = Mage::getResourceModel('catalog/category_tree')->loadNode($categoryid)->loadChildren(1)->getChildren();

But to get products none of the normal ways work

$categoryid = 14;
$category = Mage::getModel('catalog/category')->load($categoryid);
$collection1 = $category->getProductCollection(); 
$collection2 = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);

NO RESULTS in either results when the category id is outside the main tree. (both work fine for a category inside the tree). I think each time we end up in product_collection's addCategoryFilter which applies all sorts of limitations and has, as far as i can tell, no parameter to help bypass.

So far I have found the solution in Array of product ids for a given category (ignoring is_anchor) at least gives me the IDs

$categoryid = 14;
$category = Mage::getModel('catalog/category')->load($categoryid);
$positions = Mage::getResourceModel('catalog/category')->getProductsPosition($category);
$includeIDs = array_keys($positions);

but this feels very diagonal which makes me nervous. but it gets me a way to fetch products!

Is there a better way? A way that uses the category and product collections "properly" but bypasses the limitations of addCategoryFilter?

  • Edit: it is flabbergasting to me that $category->getProductCollection(); doesnt work but $category->getProductsPosition() does. This is because of the call to addCategoryFilter and so is really hard to bypass elegantly. Grr at that! – iphigenie Mar 1 '18 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.