Often you want to display customer testimonials on an e-Commerce site. With Magento, you can simply create a CMS block, add your testimonial text and display it in the left or right sidebar. That’s easy but also boring for site visitors since they will only ever see the one testimonial.
Of course, you can create several CMS blocks and show them on different pages but still, they will always be “glued” to the pages you set them to.
The next logical idea is to work out a way to display a random testimonial on every page refresh. In the first part of this tutorial series, I will show you a quick way using a .phtml file and layouts. In the second part, I’m going to turn what I covered here into a Magento extension with a few nifty enhancements.
Quick Version
To achieve our intent, we will need to accomplish the following:
- Create a phtml file which will display our CMS block
- Create a CMS block “on the fly” in our code
- Work out how to designate which CMS blocks contain our testimonials
- Set where to show the block
The first task is easy, simply create a folder in your template:
app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/myrandom/
Create your empty phtml file, say, random_testimonial.phtml.
Point number two is pretty straight forward too. A quick web search will give you plenty of pointers on how to dynamically create Magento blocks in code (within a phtml file):
$this->getLayout()->createBlock('cms/block')->setBlockId( [block_identifier] );
The third task is to work out some convenient naming convention for your CMS block identifiers. For example, you could designate a specific prefix to your blocks you want to be part of the gambling game, like: “testimonial”.
So, create several blocks with block ID’s of:
- testimonial-1
- testimonial-2
- testimonial-3
- etc…
Before we move onto point four, we need to fill in the code for our phtml template now:
<?php
$_block = $this->getLayout()->createBlock('cms/block')->setBlockId($this->getIdentifier().mt_rand(1, $this->getNumber()));
if ($_block) :
?>
<div class="block block-testimonial">
<div class="block-title">
<strong><?php echo $this->getTitle(); ?></strong>
</div>
<div class="block-content">
<?php echo $_block->toHtml(); ?>
</div>
</div>
<?php endif; ?>
You’ll notice that we have some additional properties like Title, Identifier and Number.
It will become apparent in the next step where the values for these properties come from. Now, we can decide where to call the phtml block from.
We have several options:
- Layout XML file
- Layout update
- Directly in a CMS page
1. Layout XML
Edit one of your layout XML files, for example, catalog.xml, find the element: <catalog_product_view> and within the <reference name="left"> block add:
<block type="core/template" name="random.testimonial" template="myrandom/random_testimonial.phtml"> <action method="setTitle"><title>What Customers Say...</title></action> <action method="setIdentifier"><identifier>testimonial-</identifier></action> <action method="setNumber"><number>3</number></action> </block>
Let’s see what’s happening here. We are using some Magento “magic” methods to set our introduced properties like the aforementioned Title, Identifier and Number effectively passing values for these properties to our .phtml block. This means we don’t have to edit our .phtml every time we need to change these properties.
2. Layout Update
You can insert the same XML as above into one of the layout update fields in your Magento CMS pages, products or categories.
If we insert the XML into one of our category layout updates:

The result on your selected category might look something like this:

3. CMS page block
For this approach, we will use the Magento curly braces syntax to insert the block into the page itself. This is useful if you want to drop a random testimonial into your customer services pages, for example.
Simply insert:
{{block type="core/template" template="myrandom/random_testimonial.phtml" number="2" title="Others Say:" identifier="testimonial-"}}
Where you want the block to show and save.
Remember, you can create different .phtml templates depending on the layout you need and include them in a variety of areas in your site.
Originally published on magebase.com. Copyright © 2010 Magebase - All Rights Reserved.




Proud members of the 









WOW! This is fantastic… Thank you so much for sharing such a great home made tool, exactly what I was looking for…
Or, for an easy drag and drop solution, we have just released our mooTestimonials extension. It has things like:
- gravatars for reviewers
- auto footer link
- auto sidebar mini-testimonial display
- auto-approval and auto-disapproval
- simple, easily-customised design
Check: http://www.moogento.com
hi robert i am new for magento, i wanna display random testimonials in my home page and i dont understand second point in ur 1tutorial. how can i create the several blocks with different id’s, if u place some examples its lot of help ful to me. one more thing is i need to create different blocks in same .phtml or another .phtml file… below i placed my code by which i was facing problem.. can u plz help to me…
getLayout()->createBlock(‘cms/block’)->setBlockId($this->getIdentifier().mt_rand(1, $this->getNumber()));
$_block = $this->getLayout()->createBlock(‘cms/block’)->setBlockId($this->getIdentifier().mt_rand(2, $this->getNumber()));
$_block = $this->getLayout()->createBlock(‘cms/block’)->setBlockId($this->getIdentifier().mt_rand(3, $this->getNumber()));
if ($_block) :
?>
getTitle(); ?>
toHtml(); ?>
One of the features we wanted our new home to
havewas a Great Room; a wide open space where any
number of people can gather.We looked everywhere
for a rug large enough for the room. It wasn’t
until we found Medallion,who specializes in
oversized and palace sized rugs, that we found what
we were looking for.
getTitle(); ?>
toHtml(); ?>
For this approach, we will use the Magento curly braces
syntax to insert the block into the page itself. This is
useful if you want to drop a random testimonial into your
customer services pages, for example.
getTitle(); ?>
toHtml(); ?>
It will become apparent in the next step where the values
for these properties come from. Now, we can decide where to
call the phtml block from.
The block ID is what you use in the Identifier field when you create a CMS static block. To create multiple testimonial blocks, create them using Identifiers like: testimonial-1, testimonial-2, testimonial-3, etc.
You can then show a random block using the methods in the article.
hi robert i am new for magento, i wanna display random testimonials in my home page and i dont understand second point in ur 1tutorial. how can i create the several blocks with different id’s, if u place some examples its lot of help ful to me. one more thing is should i need to create different blocks in same .phtml or another .phtml file…can u plz help to me.
Thanku very much robert for ur reply and actually i am new to magento and had entered few days back. i need to display the testiomonials in my site for every refresh of a page.. really ur article very helpful. i created 3 testiomonials with testiomonial-1, testiomonial-2, testiomonial-3 in the app\design\frontend\task\themehotwater\template\myrandom\random_testiomonial and i download the random testomonial extension file which u provided in second tutorial and copy n paste files in my magento according to those folders.. those testiomonials are displaying in my page which i hav created but how could i restrict number of testiomonials to be display for a page… Means for example if i created 6 testiomonials and need to display 3 testimonials each time, when i refresh the page remain 4, 5, 6 testimonial will display on page instead 1,2,3 testimonials..
Thanks in advance,
rahul
here i placed my random_testiomonial code and i created 3 testimonials but don’t know whether it is right or wrong…. if i made any mistake could u plz give step by step procedure how to create multiple testiomonial blocks and its very helpfull to me………
getLayout()->createBlock(‘cms/block’)->setBlockId($this->getIdentifier().mt_rand(1, $this->getNumber()));
$_block = $this->getLayout()->createBlock(‘cms/block’)->setBlockId($this->getIdentifier().mt_rand(2, $this->getNumber()));
$_block = $this->getLayout()->createBlock(‘cms/block’)->setBlockId($this->getIdentifier().mt_rand(3, $this->getNumber()));
if ($_block) :
?>
getTitle(); ?>
toHtml(); ?>
One of the features we wanted our new home to
havewas a Great Room; a wide open space where any
number of people can gather.We looked everywhere
for a rug large enough for the room. It wasn’t
until we found Medallion,who specializes in
oversized and palace sized rugs, that we found what
we were looking for.
getTitle(); ?>
toHtml(); ?>
For this approach, we will use the Magento curly braces
syntax to insert the block into the page itself. This is
useful if you want to drop a random testimonial into your
customer services pages, for example.
getTitle(); ?>
toHtml(); ?>
It will become apparent in the next step where the values
for these properties come from. Now, we can decide where to
call the phtml block from.
Thanks,
rahul.
@rahul
Please note that we don’t provide personal support for our tutorials. The tutorials require some knowledge of PHP, Object Oriented principles, Magento, etc depending on the topic. This tutorial, for example, is labeled Intermediate so it assumes that you have reasonable technical knowledge to be able to implement the material covered here.
In your case, it is matter of understanding the mt_rand() function. If you have 6 testimonials so, I assume, you would set the number parameter to 6. You are specifying in your 3 calls, the following:
This will generate random numbers from 1 to 6, then from 2-6 and the last call from 3-6. Here is the php documentation on mt_rand, make sure you read and understand the function.
Hopefully this explanation will lead you to the solution for your situation. Also, please don’t post long pieces of code without using the markup or pastebin service as explained below the comment box. As you can see, the code you pasted has lost most of its text and is unreadable.
hi robert this article is very superb working like a charm.. but it not displaying my block titles, always displays ” What Customers Say…” for every testiomonial when page refresh. how could i get those titles which i give in cms static blocks title field… thanks for u r article buddy……..
Thanks,
rahul.
Thanks a lot man, this tutorial very useful for me. please continue …..
Hi, and thanks for such a good tutorial.
I have installed the extension and it works. But I can not make it to show on the home page.
Best regards