How to bind an action after customer login success using Magento?
-
14-04-2021 - |
سؤال
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"
}
}
?>
المحلول
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"
}
}
نصائح أخرى
try
class Lpf_ModuleCookie_Model_Observer
{
public function customerLogin()
{
return Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::helper('checkout/cart')->getCartUrl());
}
}