2

I am trying to create an observer on a specific event to save on session the url referrer.

I created this structure: http://screencast.com/t/TG7brmUi

The module seems OK, I can see on magento config. http://screencast.com/t/5d4FXIJwrGk

the code is as follows:

Luisvalencia_Affiliate.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Luisvalencia_Affiliate>
            <active>true</active>
            <codePool>community</codePool>
        </Luisvalencia_Affiliate>
    </modules>
</config>

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Luisvalencia_Affiliate>
            <version>0.0.1</version>
        </Luisvalencia_Affiliate>
    </modules>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <luisvalencia_affiliate>
                        <class>luisvalencia_affiliate/observer</class>
                        <method>captureReferral</method>
                        <type>singleton</type>
                    </luisvalencia_affiliate>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

and Observer.php

<?php
class Luisvalencia_Affiliate_Model_Observer
{
    const COOKIE_KEY_SOURCE = 'luisvalencia_affiliate_source';

    public function captureReferral(Varien_Event_Observer $observer)
    {
        $referrer=Mage::app()->getRequest()->getServer('HTTP_REFERER');
        Mage::getSingleton('core/session')->setReferrer($referrer);
        $outputMessage = Mage::getSingleton('core/session')->getReferrer();
        echo $this->__($outputMessage);
    }

    protected function _getCookieLifetime()
    {
        $days = 1;
        // convert to seconds
        return (int)86400 * $days;
    }
}

However I still see this on system.log, I dont understand.

2014-08-13T15:50:17+00:00 ERR (3): Warning: include(Mage/Luisvalencia/Affiliate/Model/Observer.php): failed to open stream: No such file or directory  in /home/theprint/public_html/preprod/lib/Varien/Autoload.php on line 93
2014-08-13T15:50:17+00:00 ERR (3): Warning: include(Mage/Luisvalencia/Affiliate/Model/Observer.php): failed to open stream: No such file or directory  in /home/theprint/public_html/preprod/lib/Varien/Autoload.php on line 93
2014-08-13T15:50:17+00:00 ERR (3): Warning: include(): Failed opening 'Mage/Luisvalencia/Affiliate/Model/Observer.php' for inclusion (include_path='/home/theprint/public_html/preprod/app/code/local:/home/theprint/public_html/preprod/app/code/community:/home/theprint/public_html/preprod/app/code/core:/home/theprint/public_html/preprod/lib:.:/usr/lib/php:/usr/local/lib/php')  in /home/theprint/public_html/preprod/lib/Varien/Autoload.php on line 93
Luis Valencia
  • 303
  • 1
  • 4
  • 15

2 Answers2

3

You need to define model in your config.xml.ie you need to put this code in your config.xml file

<global>
        <models>
            <luisvalencia_affiliate>
                <class>Luisvalencia_Affiliate_Model</class>
            </luisvalencia_affiliate>
        </models>      
</global>

Without this, Magento couldn't find the Observer file that you defined in the observer.

EDIT

This is incorrect echo $this->__($outputMessage);. You can do this only in template files. In order to echo something, you can use your own helper class. Use Mage::helper('luisvalencia_affiliate')__($outputMessage) instead. But when you use this, that means you need to define helper class also inconfig.xml. So add this code

 <global>
        <helpers>
            <luisvalencia_affiliate>
                <class>Luisvalencia_Affiliate_Helpers</class>
            </luisvalencia_affiliate>
        </helpers>      
 </global>

Location: app/code/community/Luisvalencia/Affiliate/Helpers/Data.php

<?php
class Luisvalencia_Affiliate_Helpers_Data extends Mage_Core_Helper_Abstract 
{
}
Rajeev K Tomy
  • 17,234
  • 6
  • 61
  • 103
2

Try to recompile the code

System -> Tools -> Compilation -> Run compilation process

and refresh cache.

Mohit Kumar Arora
  • 9,951
  • 7
  • 27
  • 55
Nmirach
  • 31
  • 2