开发者

Is there an event for customer account registration in Magento?

I would like to be able to run some functionality with a module that I am building whenever a customer registers an account, but I can't seem to find any event that is fired upon a ne开发者_如何学Pythonw customer registration.

Does anybody know of an event that is dispatched for that?


Whenever I'm looking for an event, I'll temporarily edit the Mage.php file to output all the events for a particular request.

File: app/Mage.php
public static function dispatchEvent($name, array $data = array())
{
    Mage::log('Event: ' . $name); //not using Mage::log, as 
    //file_put_contents('/tmp/test.log','Dispatching '. $name. "\n",FILE_APPEND); //poor man's log
    Varien_Profiler::start('DISPATCH EVENT:'.$name);
    $result = self::app()->dispatchEvent($name, $data);
    #$result = self::registry('events')->dispatch($name, $data);
    Varien_Profiler::stop('DISPATCH EVENT:'.$name);
    return $result;
}

and then perform whatever action it is I'm trying to hook into. Magento events are logically named, so scanning/sorting through the resulting logs usually reveals what I'm after.


customer_register_success is what you are looking for:

<config>
  <frontend>
    <events>
      <customer_register_success>
        <observers>
          <your_module>
            <type>singleton</type>
            <class>your_module/observer</class>
            <method>yourMethod</method>
          </your_module>
        </observers>
      </customer_register_success>
    </events>
  </frontend>
</config>


I discovered how to achieve this today. It involves using one of the generic controller events. This node in the config.xml will hook into the right event:

<events>
 ....
  <controller_action_postdispatch_customer_account_createPost>
    <observers>
     <your_module_here>...etc

The controller_action_postdispatch_REQUESTPATH event is thrown for every controller that extends Mage_Core_Controller_Front_Action (which is basically all of them) which makes it very easy to target. Ditto for controller_action_predispatch_REQUESTPATH.


I'm a bit surprised that none of the answers if solving the case completely.

Customer create can happen

  1. by url customer/account/create
  2. by register in checkout

I solved it by tracking two events:

config.xml

    <events>
        <controller_action_postdispatch_customer_account_createpost>
            <observers>
                <myextensionkey_create_account>
                    <class>myextensionkey/observer</class>
                    <method>createAccount</method>
                    <type>singleton</type>
                </myextensionkey_create_account>
            </observers>
        </controller_action_postdispatch_customer_account_createpost>
        <checkout_submit_all_after>
           <observers>
              <myextensionkey_checkout_create_account>
                    <class>myextensionkey/observer</class>
                    <method>createAccountCheckout</method>
                    <type>singleton</type>
              </myextensionkey_checkout_create_account>
           </observers>
        </checkout_submit_all_after>
    </events>

and in Observer.php

public function createAccount($observer) { ... } //Nothing special here

public function createAccountCheckout($observer) {
    if ($observer->getQuote()->getData('checkout_method') != Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER) {
            return;
    }

Edit: I changed

<controller_action_predispatch_customer_account_createpost>

into

<controller_action_postdispatch_customer_account_createpost>

because on predispatch the account is not created yet. There can be an error for example if the email already exists in the shop.


There isn't a direct event for this, but you could use the customer_save_commit_after event. This event also guarantees you that the customer is save in the shop's database. The problem with this event is that is triggered twice. Bellow is an hack that allows you to use this event - the observer function is listed:

public function customer_save_commit_after($p_oObserver) {

    $l_oCustomer = $p_oObserver->getCustomer();

    if ($l_oCustomer->isObjectNew() && !$l_oCustomer->getMyCustomKeyForIsAlreadyProcessed()) {
        $l_oCustomer->setMyCustomKeyForIsAlreadyProcessed(true);
        // new customer
    }
    else {
        // existing customer
    }

    return false;

}

Hope this helps someone!


You have to consider also when the user register on-the-fly on checkout: a Register on chekout. Thinking on this case, you can catch the "checkout_type_onepage_save_order_after" event with your own Observer class, and then this code...


if($observer->getEvent()->getQuote()->getCheckoutMethod(true) == Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER){
    (...)
}

Anybody may say: Mage_Sales_Model_Quote->getCheckoutMethod() is deprecated since 1.4!!,but:

  • If we call the ortodox method Mage_Checkout_Model_Type_Onepage->getCheckoutMethod(), waiting for something as "METHOD_REGISTER" this is executed:

    if ($this->getCustomerSession()->isLoggedIn()) {
                return self::METHOD_CUSTOMER;
            }

    ... "METHOD_CUSTOMER" is the name for a checkout with an already registrated user, not our case.... but yes!, because....

  • ...the registration operation is perfomed before "checkout_type_onepage_save_order_after" event. Then we a have a METHOD_CUSTOMER now. Ups, something inconsistent?

  • Fortunatly, the Quote remains with the original value: CHECKOUT_METHOD_REGISTER

    Any other idea for the registration on checkout?


    You can try customer_save_after, the only thing that the registration sends this event twice


    Actually there are customer_save_after and customer_save_before (magento 1.5)

    If you want to modify on-the-fly some data after form post, pick customer_save_before, change the data you want and that's all (the save action come after, so your change will be taken into account).

    $customer->save() just doesn't work in customer_save_after. (fatal error) Use this observer to run a code after customer creation which are NOT related to customer data.

    Hope that helps!


    customer_register_success

    adminhtml_customer_save_after

    these two are the default events when a customer is inserted into the database.... first event fires in frontend when a user registers and second event fires in the backend when a customer is created through admin panel...i hope you know how to register an observer for an event...hope this will help you...


    I was looking of the same thing. I believe the event is customer_register_success.

    You can find a link for all events at: http://www.nicksays.co.uk/magento_events_cheat_sheet/


    I found the event checkout_submit_all_after.

    <checkout_submit_all_after>
       <observers>
          <my_example>
             <class>my_example/observer</class>
                <method>customerRegistered</method>                        
          </my_example>
       </observers>
    </checkout_submit_all_after>
    

    In my Observer.php I get the quote object that is passed in.

    public function customerRegistered (Varien_Event_Observer $observer) {
        $quote = $observer->getQuote();
        $checkout_method = $quote->getData();
        $checkout_method = $checkout_method['checkout_method'];                      
    
        if ($checkout_method == Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER) {        
    }
    

    Do not use $quote->getCheckoutMethod() it gives you login_in instead. Not sure why. Hope this helps.


    I've discovered that the customer_login and customer_session_init events are both thrown on account create. You could register a listener for either and then check to see the create date on the account?


    The answer to this question is that there isn't an event for that.


    You can use the customer_register_success event. It is triggered after the customer is succesfully created. Here is the link of event cheat sheets. Hope it also helps you.

    http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/
    


    customer_register_success is the event dispatched after successful customer registration.
    Here's from the code from Mage/Customer/controllers/AccountController.php::454 in magento 1.8:

    protected function _dispatchRegisterSuccess($customer)
    {
        Mage::dispatchEvent('customer_register_success',
            array('account_controller' => $this, 'customer' => $customer)
        );
    }
    


    customer_save_after is the event which gets called after a new customer registration.

    Read about all the events here:

    http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/events


    event name:customer_registration_is_allowed

    I'm not sure if this is you want,you can write a observer to test

  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜