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.



Thanks, it helped me with an issue trying to put the Poll Block on any part of the page and I posted the answer here http://www.magentocommerce.com/boards/viewthread/36219/
I did exactly what you said but the poll doesn’t show up?
also tried:
{{block type=”core/template” template=”poll/active.phtml” my_poll_id=”4″}}
please help..
firstly the commas in the demo are inverted, if you copy and paste you are gaurenteed to get compatability issues.
i spent hours trying to get this to work to be resolved by something so small!
secondly if you are having trouble with it displaying then dissapearing it is a problem witth the cache easily fixed by changing some cookie settings.
hope that helped.
IT IS POSSIBLE TO DISPLAY THE POLL VIA CMS!
SELECT `main_table`.* FROM `poll` AS `main_table` – I get this error?
What do you need to modify in results.phtml to display results of poll?
Thanks!
How can i make the same poll appear on the page(in taht matter at check out)even if page refresh's..
How can i make the same poll appear on the page(in taht matter at check out)even if page refresh's..
I d like to input the poll in a block type but it doesn,t work ?
I tried {block type=”core/template” template=”poll/active.phtml” my_poll_id=”4″}} but this not function.
acn you post your solution?
Hi
I did followed same a you said but its not working in my case.
but its working on right sidebar but not show up on page…
Please help me…
Regards,
manish
Steps Followed by me
1] Insert into CMS page
{{block type=”poll/poll” template=”poll/active.phtml” my_poll_id=”2″}}
2] Copy and Paste below code on the top of appdesignfrontendDefaulttemplatepollactive.phtml
<?php
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);
} ?>
<?php if(isset($poll)) : ?>
<script type=”text/javascript”>
//<![CDATA[
function validatePollAnswerIsSelected()
{
// var options = $$('input.poll_vote');
var options = $$('#poll_id_<?php echo $poll->getId() ?> input.poll_vote');
...........continue code of active.phtml
3] Replce/Chage below code
from
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>
Please let me know if i am missing any step…
Your Help is appraciated…
Thanks and Regards,
Manish Patel
Hello Branko,
How are you?
i want help for community poll show up on cms pages.
i tried but i didn't get success.
i already posted steps followed by me.
please any help is appreciated.
Thanks & Regards,
Manish Patel