Magento Tutorials

How to Get All Products Programmatically in Magento

Below is the full code to get all products in Magento Admin Panel

<?php /* listing of all product */
$allproduct = Mage::getModel(‘catalog / product’)->getCollection()
    ->setOrder(‘entity_id’, ’desc’)
    ->getData();
foreach ($allproduct as $product)
{
    $obj = Mage::getModel(‘catalog / product’);
    $productid = $product[‘entity_id’];
    $_product = $obj->load($productid); // Enter your Product Id in $product_id
    $productUrl = $_product->getProductUrl();
?>
<li><a href=”<?php echo $productUrl ?>”><?php echo $_product->getName(); ?></a></li>
<?php
} ?>

Get all products under a category in Magento

In case you have to get all products in a category, use this code:

<?php
# Uncomment the next three lines if you're calling this outside of Magento
#require_once './app/Mage.php';
#umask(0);
#Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
# If you're calling this from inside a phtml file then you can use something like this to get the category ID
#$category_id = $this->getCurrentCategory()->getId();
# or use the following line:
$category_id = 1; # Change category ID here.
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addCategoryFilter($category)->load();
foreach ($products as $product)
{
    echo '<a href="' . $product->getProductUrl() . '">' . $product->getName() . "</a><br>";
}
?>

Hope that helps!

A knowledge craver who always strive to be wiser everyday.

avatar
  Subscribe  
Notify of