Magento Tutorials

How to Get All Products Grammatically in Magento

magento-get-all-products

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

1
2
3
4
5
6
7
8
9
10
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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!

0 Comments

Dom

A knowledge craver who always strive to be wiser everyday.

    avatar
      Subscribe  
    Notify of