Skip to content

Instantly share code, notes, and snippets.

@justindocanto
Created June 1, 2015 21:56
Show Gist options
  • Save justindocanto/9ce1ece82deb64b1b3d5 to your computer and use it in GitHub Desktop.
Save justindocanto/9ce1ece82deb64b1b3d5 to your computer and use it in GitHub Desktop.
Magento - get all the products in a category
<?php
// Get products from within a category
function getCatProds($catid) {
$baseURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$category = new Mage_Catalog_Model_Category();
$category->load($catid); // this is your category id
$collection = $category->getProductCollection();
foreach($collection as $product) {
$id[] = $product->getId();
}
$html = "<ul'>";
foreach($id as $id){
$product = Mage::getModel('catalog/product')->load($id); /* Load each product by ID*/
$name = trim($product->getName());
$link = Mage::getModel('catalog/product_url')->getUrlPath($product);
$subtitle = trim($product->getData('subtitle')); // get attribute text
$html .= "<li><a href='".$baseURL.$link."' title='".$name."'><strong>".$name."</strong> (".$subtitle.")</a></li>";
}
$html .= "</ul>";
echo $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment