Daily Archives: February 25, 2014

Filter magento product collection by multiple categories

I spent quite a bit of time on this today and wanted to share the solution as it was not easily found on Google. I have a product collection and want to filter it by multiple categories… pretty much all sub categories of a given parent category. I tried using built in magento functions like addAttribteToFilter and addFieldToFilter and they both result in a “Call to a member function getBackend() on a non-object” error.

I finally found a solution on this site http://www.catgento.com/filter-product-collection-by-multiple-categories-in-magento/ . An example of my final code is below

parentCategoryId = 9;
$childCategories = Mage::getModel('catalog/category')->getCategories($parentCategoryId);
foreach($childCategories as $category) {
	$categoryIds[] = $category->getId();
//get newest used tanning bed product
$collection = Mage::getModel('catalog/product')->getCollection();
foreach ($categoryIds as $k => $cat) {
	$filter[]['finset'] = $cat;
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')

Add custom attribute to Magento’s getLoadedProductCollection() on product grid and list view 2

You have a custom attribute that needs to be accessed from the grid or list view and for the life of you it just won’t load with the collection.  You’ve tried something like

$_productCollection = $this->getLoadedProductCollection()->addAttributeToSelect('my_attribute_key')->load();

and it just doesn’t work.  If you’re like me on this issue, you’re just working too hard for the solution.  The way to do it is simply go into the admin and edit the attribute and change the “Used in product listing” setting to “yes”.  That’s it.  Hopefully I’ll save somebody a couple hours 🙂