How to get products from a particular category in magento ecommerce

余生颓废 提交于 2019-11-27 20:16:25

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')
 ;
Mukesh Chapagain

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

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

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.

$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.

emanuel

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.

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---
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!