6

I'm using Magento 1.4.1.1. After logout success, customer is rooted to "logout.phtml" first and "home page" later.

Is there anyway I could make Magento run couple of "php" codes after "login" success ?

Please don't hesitate to provide code samples as I'm new in Magento.

/var/www/app/etc/modules/Lpf_ModuleCookie.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Lpf_ModuleCookie>
            <active>true</active>
            <codePool>local</codePool>
        </Lpf_ModuleCookie>
    </modules>
</config>

/var/www/app/code/local/Lpf/ModuleCookie/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Lpf_ModuleCookie>
            <version>0.1</version>
        </Lpf_ModuleCookie>
    </modules>
    <events>
        <customer_login>
            <observers>
                <customer_login>
                    <type>model</type>
                    <class>ModuleCookie/observer</class>
                    <method>customerLogin</method>
                </customer_login>
            </observers>
        </customer_login>
    </events>
</config>

/var/www/app/code/local/Lpf/Observer.php

<?php
class Lpf_ModuleCookie_Model_Observer
{
     /**
      * Run couple of 'php' codes after customer logs in
      *
      * @param Varien_Event_Observer $observer
      */
     public function customerLogin($observer)
     {
         Mage::log(__METHOD__ . '() Hello!'); // Remove afterwards. Check your var/log/system.log to see if came to this point
         $customer = $observer->getCustomer();
         // "run couple of 'php' codes"
     }

}
?>
7

On the Mage_Customer_Model_Session model's method setCustomerAsLoggedIn() an event customer_login is being dispatched. I guess you need to hook in on that event. You could do this as follows.

Add the event to your module's config.xml file (app/code/local/Lpf/ModuleCookie/etc/config.xml):

<?xml version="1.0"?>
<config>
    <modules>
        <Lpf_ModuleCookie>
            <version>0.1</version>
        </Lpf_ModuleCookie>
    </modules>
    <global>
        <models>
            <lpf_modulecookie>
                <class>Lpf_ModuleCookie_Model</class>
            </lpf_modulecookie>
        </models>
    </global>
    <frontend>
        <events>
            <customer_login>
                <observers>
                    <lpf_modulecookie_customer_login>
                        <type>model</type>
                        <class>lpf_modulecookie/observer</class>
                        <method>customerLogin</method>
                    </lpf_modulecookie_customer_login>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

Now create a model Lpf_ModuleCookie_Model_Observer (app/code/local/Lpf/ModuleCookie/Model/Observer.php). Add a customerLogin() method to the class:

class Lpf_ModuleCookie_Model_Observer
{

     /**
      * Run couple of 'php' codes after customer logs in
      *
      * @param Varien_Event_Observer $observer
      */
     public function customerLogin($observer)
     {
         Mage::log(__METHOD__ . '() Hello!'); // Remove afterwards. Check your var/log/system.log to see if came to this point
         $customer = $observer->getCustomer();
         // "run couple of 'php' codes"
     }

}
  • Thanks for your reply. In what location should I put the class "class Mycompany_Mymodule_Model_Observer" ? I don't find "config.xml". Do you know where it is located ? – jolyqr Feb 27 '15 at 11:19
  • You need to learn how to write your own module. There's lots of tutorials online about his: google.nl/search?q=magento%20create%20your%20own%20module pick one. You will find out what "config.xml" is and where to put Model classes. I added the path's to the files I mentioned in my answer to give you some directions. – 7ochem Feb 27 '15 at 11:24
  • I have followed everything. I can't see whether my new module works fine as I don't find "var/log/system.log". Could you tell me where it is located ? – jolyqr Feb 27 '15 at 13:58
  • Logging should be turned on in System > Configuration > Developer > Log Settings > Enabled = "Yes". Also var/ should be writable. – 7ochem Feb 27 '15 at 14:00
  • No "system.log" file is created after login success. I can't find what went wrong. I have provided my modules files... – jolyqr Feb 27 '15 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.