2

I'm trying to execute a java-script command when the customer successfully registers an account, I tried the following, but the java-script command doesn't get executed.Used code

Does somebody now how I can achieve this? Thank you in advance.

[EDIT] I actually fixed it myself, I wanted to send some customer data to the html page via javascript. If you want to achieve this, please read my answer.

2
  • Hi and welcome to Magento SE, feel free to check out this post on how to ask a question: magento.stackexchange.com/help/how-to-ask . Please provide more information such as what do you want to do exactly ? Redirect your customer ? Please explain what your final code is supposed to do ? Oct 6 '16 at 15:18
  • If you trace the code when a customer is registered successfully, you'll see that it will be redirect to a URL $this->_redirectSuccess($url); where $url is by default customer/account/index or the URL param success_url, see Mage_Core_Controller_Varien_Action ::_redirectSuccess(). So, in order to execute your js, you'll need to find a way to always redirect to the page where you have your js embedded. You can do this by hard-setting the success_url.
    – kiatng
    Oct 7 '16 at 5:17
2

After some more digging, and thanks to the answers from kiatng and lalit mohan, I found the answer.

I wanted to list all customer details to the page via javascript.

In the mage.php file, located at Magento\apps\magento\htdocs\app:

 public static function dispatchEvent($name, array $data = array())
{
    //Custom code, this is being executed when the customer succesfully registers
    if($name == "customer_register_success")
    {
        $customer = $data['customer'];
        $customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData();

        $out = "";
        foreach( $customerData as $key => $value ) {
            //this prevents the password hash to be show in the session  
            if(!$key == "password_hash")
            {
                $out .= "|" . $key . ':' . $value;
            }
        }

        //Put customer vallues in the sessions
        session_start();
        $_SESSION["data"] = $out;
    }

  //Logging to htdocs/var/log/system.log
  Mage::log('Event fired:  ' . $name . "__" . $data);

  //Default code (this was already there)
  Varien_Profiler::start('DISPATCH EVENT:'.$name);
  $result = self::app()->dispatchEvent($name, $data);
  Varien_Profiler::stop('DISPATCH EVENT:'.$name);
  return $result;

}

Then in your javascript:

var customerdata = "<?php session_start(); echo $_SESSION["data"]; ?>";

This will output all customer data to a string.

1
  • Did you add the js code in the same php file?
    – arqam
    Sep 3 '19 at 9:01
1

you can run profiler to debug problem by edit temporarily Mage.php file. so you can output of all events for 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;
}
1

In order to execute your js, you'll need to find a way to always redirect to the page where you have your js embedded. You can do this by hard-setting the success_url. One way to do this is to listen to the event customer_success_event, as indicated in your attached image. After you have set this up, then the code is quite simple:

public funstion setCustomerRegisterSuccessUrl($observer)
{
    $successUrl = Mage::getUrl('your_module/your_controller/your_action');
    $controller = $observer->getEvent()->getAccountController();
    $controller->getRequest()->setParam('success_url', $successUrl);
}

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.