Magento new user registration - inject own code

2019-09-01 05:55发布

问题:

I'm trying to make a loose connection between a Magento installation and another application. In particular, there are only two things I care about at the moment - customers and their purchases. What I edited thus far is the file app/code/core/Mage/Customer/controllers/AccountController.php - function createPostAction() is augmented to send the customer's info to the other application. Another modification was made in success.phtml file to send out the details of the order once it's placed.

The problem I'm having is that if a user just places items in a cart (as a guest) and then registers as part of the checkout process - the customer's record in the second application is never created. It only works if the user first explicitly registers, and then checks out separately.

I suppose AccountController.php may be the wrong file to modify to achieve my desired result, which file should I use instead?

Edit: I am not including the code samples, as it's largely irrelevant - the problem is not with the code, but with the fact that it's apparently in the wrong place. Where would be a good place to add custom code which should run when a new customer is registered?

回答1:

This is a bit tricky to do cleanly in Magento but possible to achieve. I've had the same task a while ago and solved using observers

First you need to create an observer which listens controller_action_postdispatch event:

<events>
    <controller_action_postdispatch>
        <observers>
            <yourmodule_anything>
                <type>singleton</type>
                <class>yourmodule/observer</class>
                <method>someMethod</method>
            </yourmodule_anything>
        </observers>
    </controller_action_postdispatch>
</events>

Then in your observer method you can check action names as follows

<?php
$action = $observer->getEvent()->getControllerAction();
if ($action->getFullActionName() == 'customer_account_createpost') {
    if (Mage::getSingleton('customer/session')->isLoggedIn()) { // this check allows to determine if customer session was created which means successfull registration
    }
}

Something similar can be done for checkout registration. In general observers are great (and proper) way to extend Magento functionality. Read more about them here



回答2:

Anton S is right - you should check if Magento is firing an event when this occurs. If so, your code should respond to that event.

If that is not the case, or you need custom logic to run before the account is created, please consider overriding/overloading the controller using a custom module: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller

This keeps the original core code intact. It essentially tells Magento to use your custom controller instead. If the method it needs to execute is not found in your controller, it will use the code from the core instead.



标签: php magento