Posted By

tomle123 on 04/24/13


Tagged

category filter magento featured


Versions (?)

filter out by category


 / Published in: PHP
 

filter out by category ID in Magento

  1. //get the current category
  2. $_currentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId());
  3.  
  4. //spit out the collection filtered by store then by category
  5. $_productCollection = Mage::getResourceModel('catalog/product_collection')
  6. ->addStoreFilter()
  7. ->addCategoryFilter($_currentCategory);
  8.  
  9. //get all categories
  10. $categories = Mage::getModel('catalog/category')->load($parent_category_id)->getAllChildren();
  11.  
  12. foreach ($categories as $_category)
  13. {
  14. $cur_category = Mage::getModel(���¢�¯�¿�½�¯�¿�½catalog/category���¢�¯�¿�½�¯�¿�½)->load($_category->getId());
  15. //get all products associated with the current category
  16. $products = Mage::getResourceModel(���¢�¯�¿�½�¯�¿�½catalog/product_collection���¢�¯�¿�½�¯�¿�½)
  17. ->addCategoryFilter($_category)
  18. ->addAttributeToSelect(���¢�¯�¿�½�¯�¿�½some_attributes���¢�¯�¿�½�¯�¿�½);
  19. foreach ( $products as $productModel )
  20. {
  21. $_product = Mage::getModel(���¢�¯�¿�½�¯�¿�½catalog/product���¢�¯�¿�½�¯�¿�½)->load($productModel->getId());
  22. //do something useful with this $_product object
  23. //$_product->getName(), $_product->getId() etc.
  24. }
  25.  
  26. }

Report this snippet  

You need to login to post a comment.