0
votes

I've read the other posts about this subject and tried loads of things but cant get a Variable from my controller file to the Checkout. I have very little experience with creating modules so followed a guide to create the following

app ▸ code ▸ local ▸ Creare ▸ AgeRestricted ▸ etc ▸ config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Creare_AgeRestricted>
            <version>0.1.0</version>
        </Creare_AgeRestricted>
    </modules>
    <global>
        <events>
            <payment_method_is_active>
                <observers>
                    <age_restricted>
                        <type>singleton</type>
                        <class>AgeRestricted/observer</class>
                        <method>AgeRestricted</method>
                    </age_restricted>
                </observers>
            </payment_method_is_active>
        </events> 
        <models>
            <AgeRestricted>
                <class>Creare_AgeRestricted_Model</class>
                <resourceModel>agerestricted_mysql4</resourceModel>
            </AgeRestricted>
        </models>
        <sales>
            <quote>
                <item>
                    <product_attributes>
                        <age_restricted/>
                    </product_attributes>
                </item>
            </quote>
        </sales>
    </global>
</config>

Macintosh HD ▸ WWW-local ▸ dexam ▸ htdocs_live ▸ app ▸ code ▸ local ▸ Creare ▸ AgeRestricted ▸ Model ▸ Observer.php

<?php

class Creare_AgeRestricted_Model_Observer
{
    public function AgeRestricted(Varien_Event_Observer $observer)
    {
    Mage::register('feedback', $AgeRestricted);
       $event           = $observer->getEvent();
           $method          = $event->getMethodInstance();
           $result          = $event->getResult();
       $AgeRestricted        = false;

        foreach (Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems() as     $item)
        {
            if($item->getProduct()->getAgeRestricted()){
               $AgeRestricted = true;
             }

        }

    }

}

Notice I added the line:

Mage::register('feedback', $AgeRestricted);

then im trying to call this with:

echo Mage::registry('feedback');

in file: app/design/frontend/dexam/default/template/checkout/onepage/payment.phtml

but it displays nothing at all. I've tested the location in payment.phtml is working.. my test text appears but 'feedback' is not calling the $AgeRestricted.

I tried echo $AgeRestricted; in my controller, which echo'd lots of '1's at the top of my screen so I can see the variable contains the true value.

How can I call $AgeRestricted in payment.phtml?

Many thanks!! Daz

2
You have to put Mage::register('feedback', $AgeRestricted); after the initialization of the variable $AgeRestricted = false;adrien54
Thanks, I tried this but i'm getting the error - Mage registry key "feedback" already exists. I'm certain its because the same code is running over and over. I tried the if statment below too but that also gives the key already exists error.user2864591

2 Answers

0
votes

<?php

class Creare_AgeRestricted_Model_Observer
{
    public function AgeRestricted(Varien_Event_Observer $observer)
    {
    
       $event           = $observer->getEvent();
           $method          = $event->getMethodInstance();
           $result          = $event->getResult();
       $AgeRestricted        = false;

        foreach (Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems() as     $item)
        {
            if($item->getProduct()->getAgeRestricted()){
               $AgeRestricted = true;
             }

        }

    if(!Mage::register('feedback')){
      	Mage::register('feedback', $AgeRestricted); // put this line here after all processing
    }

    }

}

Hope this will help you out

0
votes

You can use magento checkout sessions to set the value from the observer to payment file

<?php

class Creare_AgeRestricted_Model_Observer
{
    public function AgeRestricted(Varien_Event_Observer $observer)
    {

       $event           = $observer->getEvent();
           $method          = $event->getMethodInstance();
           $result          = $event->getResult();
       $AgeRestricted        = false;

        foreach (Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems() as     $item)
        {
            if($item->getProduct()->getAgeRestricted()){
               $AgeRestricted = true;
             }

        }

    if($AgeRestricted){
        Mage::getSingleton('checkout/session')->setRestictedAge($AgeRestricted);
    }

    }

}

In your payment.phtml

<?php $session = Mage::getSingleton('checkout/session');?>
<?php echo $session->getRestrictedAge();?>