code_junkie

Magento products by categories

Nov 12th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2. $_cat = $this->getCurrentCategory();
  3. $_parent = $_cat->getParentCategory();
  4. $_categories = $_parent->getChildren();
  5.  
  6. /* @var $category Mage_Catalog_Model_Category */
  7. $collection = Mage::getModel('catalog/category')->getCollection();
  8. /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
  9. $collection->addAttributeToSelect('url_key')
  10. ->addAttributeToSelect('name')
  11. ->addAttributeToSelect('is_anchor')
  12. ->addAttributeToFilter('is_active', 1)
  13. ->addIdFilter($_categories)
  14. ->setOrder('position', 'ASC')
  15. ->joinUrlRewrite()
  16. ->load();
  17.  
  18. $productCollection = Mage::getResourceModel('catalog/product_collection');
  19. $layer = Mage::getSingleton('catalog/layer');
  20. $layer->prepareProductCollection($productCollection);
  21. $productCollection->addCountToCategories($collection);
  22. // $productCollection should be ready here ;-)
  23. ?>
  24.  
  25. $productCollection = Mage::getResourceModel('catalog/product_collection')
  26. ->addCategoryFilter($category);
  27.  
  28. $categoryId = 123; // a category id that you can get from admin
  29. $category = Mage::getModel('catalog/category')->load($categoryId);
  30.  
  31. $products = Mage::getModel('catalog/product')
  32. ->getCollection()
  33. ->addCategoryFilter($category)
  34. ->load();
  35.  
  36. print_r($products);
Add Comment
Please, Sign In to add comment