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 ? Commented Oct 6, 2016 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. Commented Oct 7, 2016 at 5:17

3 Answers 3

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.

answered Oct 7, 2016 at 15:09
1
  • Did you add the js code in the same php file? Commented Sep 3, 2019 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;
}
answered Oct 6, 2016 at 16:22
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);
}
answered Oct 7, 2016 at 5:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.