Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

using “related.phtml” to show 8 random products
 
Webunity
Member
 
Total Posts:  55
Joined:  2008-06-17
 

I would like to use a template (i started with related.phtml since that is allready at the right place) to show 8 random products.

So far i came up with this:

//
    // Get random products
    $storeId Mage::app()->getStore()->getId();
    
$arrItems Mage::getModel('catalog/product')
                                ->
setStoreId($storeId)
                                ->
getCollection()
                                ->
addAttributeToSelect('is_active')
                                ->
addAttributeToSelect('is_salable')
                                ->
addAttributeToSelect('is_in_stock')
                                ->
getItems();
    
$arrRandom array_rand($arrItems8);

    
$arrProducts = array();
    foreach (
$arrRandom as $intRandom{
        $arrProducts[] 
$arrItems[$intRandom];
    
}
    printProductList
($this$arrProducts);

However; i want to limit the returned products to active, salable and in stock items only. The Query above does not seem to do this. Also, the attributes (e.g. “product name") don’t seem to be filled this way. Any tips?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Webunity
Member
 
Total Posts:  55
Joined:  2008-06-17
 

Anybody has any idea on how to fix this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

try this code to get the collection:

$collection Mage::getModel('catalog/product')->getCollection();      
                ->
addMinimalPrice()
                ->
addFinalPrice()
                ->
addTaxPercents()
                ->
addStoreFilter()
                ->
getSelect()->order('rand()')
                ->
addStoreFilter();                
                ->
setPage(18);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

foreach(
$collection AS $_product){
    var_dump
($_product->toArray());
}

Cheers

 
Magento Community Magento Community
Magento Community
Magento Community
 
nasha
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2009-04-15
 

@zentrale
Can you explain me little more.where to put that code.i am using same code in view.phtml but nothing displayed.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dolito
Jr. Member
 
Total Posts:  22
Joined:  2009-08-24
Stuttgart, Germany
 

Hi,

the context should be ok. Maybe you want to check Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
( file: app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php

Also good resource to take a look at: http://framework.zend.com/manual/en/zend.db.select.html Worth a glance!

Greetz dolito

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top