Reusing Magento poll on any page or any block

poll-featured-image

On very rare occasions we get to work with polls. Most of our Magento projects end up with polls removed from the layout for one reason or the other – mostly because we rely on social networks instead for adding ‘social’ aspect to stores.

Nonetheless, sometimes client wants to have polls on his site. Magento has default poll system, which does it’s job. To an extent. What do we do if we want to add multiple polls to the site or choose which poll we want to display?

I’ll show you. The procedure is not complicated. It depends on what you want to achieve, and how you want to proceed.

CMS page

One option is adding polls through CMS page, by calling the poll block and loading a specific poll by it’s ID. Let’s do that on the ‘About Us’ page.

Open the CMS page in Magento administration (CMS->Pages->click on ‘About Us’), and under Design tab, paste this piece of code:

<reference name="content">
<block type="poll/activePoll" name="custom.poll">
<action method="setPollId"><pollId>1</pollId></action>
<action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
<action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
</block>
<block type="poll/activePoll" name="custom.poll2">
<action method="setPollId"><pollId>2</pollId></action>
<action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
<action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
</block>
</reference>

This layout uses Magento’s default poll block methods – firstly setting the PollId to an ID we have chosen (highlighted lines in this example), and sets appropriate poll templates. Just keep in mind that, if you want to add multiple polls, be sure to set an unique name attribute to each block.

You should see two polls printed in the ‘About Us’ page.

10250-2

Programatically

Let’s also cover adding polls programatically to any page we like. In this example, we’ll add a poll to left column of ‘3 columns layout’ template.

Open app/design/frontend/base/default/template/page/3columns.phtml

<?php
	$poll = $this->getLayout()->createBlock('poll/activePoll');
	$poll->setPollId(2);
	$poll->setPollTemplate('poll/active.phtml', 'poll');
	$poll->setPollTemplate('poll/result.phtml', 'results');
	echo $poll->toHtml();
?>

Add the code above just before

<?php echo $this->getChildHtml('left') ?>

The result:
10250-1

Note that adding the poll this way means that it would load regardless of poll start and end date.

This means that the poll would show, but you wouldn’t be able to place a vote, since when placing a vote, there is a piece of code in place that checks whether the poll is expired.

That’s it for today, a quick way of adding polls to any page in Magento.

Note: This is a revamp of an article originally written in May 2009.


1 comment

  1. These is good tutorial and it helps a lot, but is it possible to display one submit button for all polls.
    Example:
    First click all polls check button then click one submit button and all polls got submit.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.