Allbutlost
Allbutlost

Reputation:

How to get products from a particular category in magento ecommerce

I'd like to get a list of random products from the same category as the current product for displaying within the product view - so far all I've dug up is

Magento products by categories

Does anyone know how to do this?

Upvotes: 10

Views: 95300

Answers (6)

Josh Pennington
Josh Pennington

Reputation: 6408

You basically load up the category, get the Product Collection and then filter appropriately.

$products = Mage::getModel('catalog/category')->load($category_id)
 ->getProductCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('status', 1)
 ->addAttributeToFilter('visibility', 4)
 ->addAttributeToFilter('special_price', array('neq' => ""))
 ->setOrder('price', 'ASC')
 ;

Upvotes: 23

safin chacko
safin chacko

Reputation: 1390

This code will helps you to get products from category id 2. And also here uses a template file list_home.phtml for the product listing.

 echo $this->getLayout()->createBlock("catalog/product_list")
    ->setCategoryId(2)->setTemplate("catalog/product/list_home.phtml")->toHtml();

list_home.phtml

<?php
$this->getChild('toolbar')->setCurrentMode('list'); //uses list mode
$_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
    ?>

    <?php if (!$_productCollection->count()): ?>
        <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
    <?php else: ?>

--use code for listing---

Upvotes: 0

emanuel
emanuel

Reputation: 21

You should instantiate a model by calling Mage::getModel('catalog/product') in this case because then you get a configured object instance, extended by any configured modules.

If you do it like new Mage_Catalog_Model_Product() this will ignore modules and bypass the Magento API.

Upvotes: 2

Chiragit007
Chiragit007

Reputation: 1646

$products = Mage::getModel('catalog/category')->load(category_id); //put your category id here
       $productslist = $products->getProductCollection()->addAttributeToSelect('*');
       foreach($productslist as $product)
       {
        echo 'price: ' . $product->getPrice() . '<br/>';
       }

This is the by far the convenient code in order to fetch product details of perticular category.Hope it helps you.

Upvotes: 3

Mukesh Chapagain
Mukesh Chapagain

Reputation: 25948

Here is the code to get products from any particular category:-

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addCategoryFilter($category);

Upvotes: 18

Dan Klassen
Dan Klassen

Reputation: 116

what I ended up doing is in app/design/frontend/default/theme_name/template/catalog/product/list_random.phtml

doing something like:

<?php 
$_categories=$this->getCurrentChildCategories();

$_category = $this->getCurrentCategory();
$subs = $_category->getAllChildren(true);
$result = array();
foreach($subs as $cat_id) {
    $category = new Mage_Catalog_Model_Category();
    $category->load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
        $result[] = $product->getId();
    }

}
shuffle($result);
?>

this will get you an array of product id's. You can loop through them and create products on the fly using:

<?php 
$i=0; 
foreach ($result as $_product_id){ 
    $i++;
    $_product = new Mage_Catalog_Model_Product();
    $_product->load($_product_id);
    //do something with the product here
}?>

then, create a static block in the cms with the following content

{{block type="catalog/navigation" template="catalog/product/list_random.phtml"}} 

Finally, in the Catalog->Manage categories section, choose the category, then the display settings tab. Switch the display mode to "Static block and products" and then choose your block from the drop list.

And that should do it.

Upvotes: 7

Related Questions