1

I'm trying to add a script after the customer created a account.

So far I can target the event. But after the event is fired I can't seem to add the script to the next page load. Can someone point me in the right direction?

Cool > Marketing > etc > config.xml

<config>
<modules>
    <cool_marketing>
        <version>1.0.0</version>
    </cool_marketing>
</modules>
<frontend>
    <events>
        <controller_action_postdispatch_customer_account_createpost>
            <observers>
                <cool_marketing_create_account>
                    <class>cool_marketing_model_observer</class>
                    <method>createAccount</method>
                    <type>singleton</type>
                </cool_marketing_create_account>
            </observers>
        </controller_action_postdispatch_customer_account_createpost>
        <checkout_submit_all_after>
            <observers>
                <cool_marketing_checkout_create_account>
                    <class>cool_marketing_model_observer</class>
                    <method>createAccountCheckout</method>
                    <type>singleton</type>
                </cool_marketing_checkout_create_account>
            </observers>
        </checkout_submit_all_after>
    </events>
</frontend>

Cool > Marketing > Model > Observer.php

<?php
class Cool_Marketing_Model_Observer 
{

public function createAccount($observer) {
    $this->loadLayout();

    $block = $this->getLayout()->createBlock(
        'Mage_Core_Block_Template',
        'account_create_success',
        array('default' => 'cool/account_create_success.phtml')
    );

    $this->getLayout()->getBlock('content')->append($block);


    $this->renderLayout();
}
1

Your intentions may be misunderstood, but I think regardless of the understanding between the question and solution, you should know the registration success is likely not a template, but instead a session success message.

I'm going to opt out of explaining Magento's framework for doing this, but I'll try to provide a little guidance.

What it seems you may want to investigate, instead, is a bit more complex than you might think, but consider that you may need to author a custom extension. The custom extension is a bit involved, where you would need to rewrite a controller, create/rewrite a block or two, and then typical usage of the theme layouts and templates to render the page. The layouts would need a new handle for your new account registration success route.


An alternative method may also include injecting content via AJAX, but at this point, question the importance of what you are trying to do and see if it can be done elsewhere in the sign-up process.


Observer method

If I am mistaken about your intentions, or that was just example code, then the event you may need to observe is: <customer_register_success> which can be referenced below by following the protected function _dispatchRegisterSuccess($customer) leading to the dispatching of the event you may be aiming to observe.

Within Mage/Customer/controllers/AccountController.php we have:

/**
 * Create customer account action
 */
public function createPostAction()
{
    // ... code removed for readability

    $customer = $this->_getCustomer();

    try {
        $errors = $this->_getCustomerErrors($customer);

        if (empty($errors)) {
            $customer->cleanPasswordsValidationData();
            $customer->save();


            // NOTE: This may be your desired observer.
            $this->_dispatchRegisterSuccess($customer);


            $this->_successProcessRegistration($customer);
            return;
        } else {
            $this->_addSessionError($errors);
        }
    } catch (Mage_Core_Exception $e) {
        $session->setCustomerFormData($this->getRequest()->getPost());
        if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
            $url = $this->_getUrl('customer/account/forgotpassword');
            $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
        } else {
            $message = $this->_escapeHtml($e->getMessage());
        }
        $session->addError($message);
    } catch (Exception $e) {
        $session->setCustomerFormData($this->getRequest()->getPost());
        $session->addException($e, $this->__('Cannot save the customer.'));
    }

    $this->_redirectError($errUrl);
}

Here is the where the dispatched event is done:

  /**
 * Dispatch Event
 *
 * @param Mage_Customer_Model_Customer $customer
 */
protected function _dispatchRegisterSuccess($customer)
{
    Mage::dispatchEvent('customer_register_success',
        array('account_controller' => $this, 'customer' => $customer)
    );
}

Final Thoughts

Let's consider that there are some development standards with observers, where observers have typical use cases that should be followed. With that said, creating a block, and rendering it within an observer, is probably not something that should be done. I have seen observers used for loading layout xml for updating it, but not so sure about rendering output.

In some cases, observers are passed objects, modified, and require a return of $this.

1
  • Nice explanation, you understood my question well. Indeed there is a bit more to it, I see now. It was not so much example code, I was using it in my new module. – Master Deo Apr 6 '18 at 12:32

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.