Menu
Web Design | SEO | Website Development

UK Based Web Agency, Magento & Wordpress Specialist (Cranleigh | Surrey)

Limit & Randomise Related Products in Easy Tabs Magento Extension

By In Magento Development, Platforms, Website Development On 13/02/2014


This is a useful little tweak to the extremely useful Easy Tabs Free Magento Extension. I love Easy Tabs and use it as much as possible.

In this particular site rather than using an auto relate extension I have manually related many products – it took ages so when it was necessary to increase page speed I needed a neat way to both limit and randomise the shown products so that they aren’t in the same order each page load.

In the code below I have stripped out a lot of what I think is unnecessary functionality, such as the buy now checkbox and wishlist etc – if you particularly want that functionality combine your code with the below.

All you need to do is to swap below code for your code in:  app/design/frontend/default/default/template/easytabs/catalogproductrelated.phtml

To change the number of products shown simply change the number in this line:  $rel_items_rand_num = 6;

<?php
$rel_items_num = $this->getItems()->getSize();
$rel_items_rand_num = 6;
$rel_items_rand_cnt = 0;
$rel_items_rand;

if ($rel_items_num > 0) {
foreach($this->getItems() as $_item) {
$rel_items_rand[] = $_item;
}
shuffle($rel_items_rand);
if ($rel_items_rand_num < $rel_items_num) {
$rel_items_rand = array_chunk($rel_items_rand, $rel_items_rand_num);
$rel_items_rand = $rel_items_rand[0];
}
}
?>
<?php if($rel_items_num): ?>
<ol>
<?php $show_index_array = range(0,($this->getItems()->getSize()-1)); ?>
<?php shuffle($show_index_array); ?>
<?php $show_index_array = array_slice($show_index_array, 0); ?>
<?php foreach($rel_items_rand as $_item): ?>
<li<?php if($_item->isComposite() || !$_item->isSaleable()): ?> class="super-products"<?php endif; ?>>
<div class="product-images">
<a href="<?php echo $_item->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(100) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" width="100" height="100" /></a>
</div>
<div class="product-details">
<a href="<?php echo $_item->getProductUrl() ?>"><?php echo substr_replace($this->htmlEscape($_item->getName()),"...", 40);?></a>
<?php echo $this->getPriceHtml($_item, true) ?>
</div>
</li>
<?php endforeach ?>
</ol>
<?php endif ?>

I Hope this helps someone out there, any questions please ask.


About the Author

Dan Vince

Yet another Web Development Professional

Related Posts

3 Comments

  • Prof 4 YEARS AGO

    Great solution! It works for me, but i notice that that $rel_items_rand_cnt isn't use and $index_iterator should be declared on top with 0.But, great work. You helped me a lot ;-)

    Reply
    • Dan Vince 4 YEARS AGO

      Hi Prof, please can add your complete code here?

      Reply
  • Dan Vince 3 YEARS AGO

    The above code now fixes a couple of bugs.

    Reply

Leave a comment