10

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?

Community
  • 1
  • 1

6 Answers6

23

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')
 ;
Josh Pennington
  • 6,418
  • 13
  • 60
  • 93
  • 1
    Maybe the best solution here, however it would be even better if you would include the full answer here with code. Just in case the URL ever stops working. – todd Dec 05 '13 at 00:13
  • This solution is probably more efficient that the accepted answer as it doesn't have to go through the entire product range, only those belonging to that category. – dayuloli Feb 04 '15 at 06:06
  • 1
    links is not found. can you check that. – Kailas Feb 25 '16 at 12:45
18

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

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addCategoryFilter($category);
demonkoryu
  • 1,247
  • 10
  • 27
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
7

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.

Dan Klassen
  • 116
  • 1
  • 6
  • 1
    just a note: the above code will get all products from the current AND sub categories. It should be pretty trivial to make it only the current category. – Dan Klassen May 13 '09 at 23:30
  • Note: you should NOT include "complex logic" in template file. You should create BLOCK with specific method for retrieving filtered products which would be used in template. Also you should use @chapagain method or `Mage_Catalog_Model_Category::getProductCollection()` which is basically the same, but it also adds STORE VIEW filter. – xyz Sep 07 '12 at 06:01
3
$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.

Chiragit007
  • 1,646
  • 2
  • 16
  • 31
2

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.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
emanuel
  • 21
  • 1
0

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---
safin chacko
  • 1,345
  • 1
  • 11
  • 18