cancel
Showing results for 
Search instead for 
Did you mean: 

Random products list

Random products list

I have the following code, which displays random products for me:

 

<?php
$numberOfItems = 15;
 
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection
    ->addStoreFilter()
    ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$candidateIds = $productCollection->getAllIds();
$numberOfProducts = count($candidateIds);
$chosenIds = [];
while ($numberOfItems) {
    $randomKey = mt_rand(0, $numberOfProducts - 1);
    if (!isset($chosenIds[$randomKey])) {
        $chosenIds[$randomKey] = $candidateIds[$randomKey];
        --$numberOfItems;
    }
}
$productCollection->addIdFilter($chosenIds);
$productCollection
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
    ->addUrlRewrite();
 
$collection= $productCollection->load();	
?>
<?php if ($collection->getSize() > 0): ?>
<div class="thetitle">
 <h2>Your recently viewed</h2>
</div>
     <div class="jcarousel-wrapper">
                <div class="jcarousel">
                    <ul>
                           <?php foreach ($collection as $_product) {  ?>
                           
                        <li><a href="<?php echo $_product->getProductUrl() ?>">
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" alt="" /></a> 
                        <div class="productName">
                        <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
                        </div> 
                        
                         <?php if($_product->isSaleable()): ?>
                        <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Basket') ?></span></span></button></p>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                   
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                      
                    </ul>
                    
                        </li>
                         <?php }?>
                    </ul>
                </div>

                <a href="#" class="jcarousel-control-prev">&lsaquo;</a>
                <a href="#" class="jcarousel-control-next">&rsaquo;</a>


            </div>
<?php endif;?>

Now, I think this is wrong as although it works, lines 1 - 27 should be in its own custom module. I have tried to create one following a guide but it didn't work.

 

 I was wondering if someone of greater experience and knowledge could help me to correct this and create a custom module or extend a relevant module for this?