I was browsing official Magento site when I came across this question: “How can I manage multiple polls on multiple pages?”. To be honest, I’ve been doing Magento related development for almost a year now and I have never, ever had the chance or need to do anything related to Polls in Magento. Sound almost crazy, but across the projects I was working on, I never had the need to use this feature, they all ended up removed from layout. Anhow. To reply to question at the begining and to present some extra information on how to use the polls on any page or any block here are some screenshots and few tips.
First we need to determine which block (view) file shows the output of Poll. That’s easy, just turn on Template Path Hints and you will see that the block that outputs the polls is the app/design/frontend/default/activecodeline/template/poll/active.phtml file. Note the activecodeline, that’s the name of my theme.
Moving on, at the top of the file do the
. Unlike most of the other files where view files are arranged to be used in context of $this, active.phmtl is a bit different.
First question a newbie might ask: “Where does $poll come from?”. Each variable you see in view file beeing used but not declared is assigned from controller to view file. If you open up the app/code/core/Mage/Poll/Block/ActivePoll.php file and look around line 80 you will find something like
$this->assign('poll', $poll) ->assign('poll_answers', $pollAnswers) ->assign('action', Mage::getUrl('poll/vote/add', array('poll_id' => $pollId)));
This is the point where controller assigned variables to our active.phtml files. Knowing this we see that if we wish to use the active.phtml in combination with some CMS page where we would call the poll like {{block type=”poll/poll” template=”poll/active.phtml” my_poll_id=”2″}} we need to code some extra stuff into the active.phtml files.
Note the use of my_poll_id=”2″ attribute in calling a block trough CMS page. This is not a standard Magento attribute for Poll object. This is something I made up. Important thing to keep in mind is that everything assigned as attribute when calling a certain block can be accessed from view file like $this->my_poll_id.
Now we have everything we need to extend our Poll block to be able to show any poll we wish on any page we wish.
We can start by adding extra code in top of the active.phtml file like
if(!isset($poll)) { $poll = new Mage_Poll_Model_Poll(); $poll->load($this->my_poll_id); $action = Mage::getUrl('poll/vote/add', array('poll_id' => $poll->getId())); $poll_answers = Mage::getModel('poll/poll_answer') ->getResourceCollection() ->addPollFilter($poll->getId()) ->load() ->countPercent($poll); }
As I said, this goes at very top of the active.phtml file. Doing so we are saying that if the $poll object does not exist, try looking for manually assigned my_poll_id attribute which we will assign trough CMS page. Inside the if logic we are creating a new instances of variables that in normal cases need to exist in page.
To make this complete, there is a one more final step we need to do. We need to handle the JavaScript. Since poll block is build with the idea of “only one exists in page”, it uses class selectors without parent id selector. So, to make this more generic, we need to change the
var options = $$('input.poll_vote');to
var options = $$('#poll_id_<?php echo $poll->getId() ?> input.poll_vote');And
<td><input type="radio" name="vote" class="poll_vote" id="vote_<?php echo $answer->getId() ?>" value="<?php echo $answer->getAnswerId() ?>" /></td>
to
<td id="poll_id_<?php echo $poll->getId() ?>"><input type="radio" name="vote" class="poll_vote" id="vote_<?php echo $answer->getId() ?>" value="<?php echo $answer->getAnswerId() ?>" /></td>
And here are the final results.
Result 1: Calling a poll block trough CMS page with manualy assigned poll id with my choice

Result 2: Calling a poll block trough CMS page with manualy assigned poll id with my choice, and with another poll already on page

Hope you find some usage of this approach. Cheers.