/ Published in: PHP
filter out by category ID in Magento
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//get the current category $_currentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId()); //spit out the collection filtered by store then by category $_productCollection = Mage::getResourceModel('catalog/product_collection') ->addStoreFilter() ->addCategoryFilter($_currentCategory); //get all categories $categories = Mage::getModel('catalog/category')->load($parent_category_id)->getAllChildren(); foreach ($categories as $_category) { $cur_category = Mage::getModel(���¢�¯�¿�½�¯�¿�½catalog/category���¢�¯�¿�½�¯�¿�½)->load($_category->getId()); //get all products associated with the current category $products = Mage::getResourceModel(���¢�¯�¿�½�¯�¿�½catalog/product_collection���¢�¯�¿�½�¯�¿�½) ->addCategoryFilter($_category) ->addAttributeToSelect(���¢�¯�¿�½�¯�¿�½some_attributes���¢�¯�¿�½�¯�¿�½); foreach ( $products as $productModel ) { $_product = Mage::getModel(���¢�¯�¿�½�¯�¿�½catalog/product���¢�¯�¿�½�¯�¿�½)->load($productModel->getId()); //do something useful with this $_product object //$_product->getName(), $_product->getId() etc. } }