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:


$numberOfItems = 15;
$productCollection = Mage::getModel('catalog/product')->getCollection();
$candidateIds = $productCollection->getAllIds();
$numberOfProducts = count($candidateIds);
$chosenIds = [];
while ($numberOfItems) {
    $randomKey = mt_rand(0, $numberOfProducts - 1);
    if (!isset($chosenIds[$randomKey])) {
        $chosenIds[$randomKey] = $candidateIds[$randomKey];
$collection= $productCollection->load();	
<?php if ($collection->getSize() > 0): ?>
<div class="thetitle">
 <h2>Your recently viewed</h2>
     <div class="jcarousel-wrapper">
                <div class="jcarousel">
                           <?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>
                         <?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; ?>
                         <?php }?>

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

<?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?




Tags (2)