Posted on

How to run a JS code as soon as a user logs in through checkout. Magento 1.9

Guys I need to run a code as soon as a user logs in through checkout. But I’m not getting it, precisely because it sends a request to “/customer/account/loginPost/” and the return is a refresh to the checkout page.

public function loginPostAction()
{
if (!$this->_validateFormKey()) {
$this->_redirect(‘*/*/’);
return;
}

if ($this->_getSession()->isLoggedIn()) {
$this->_redirect(‘*/*/’);
return;
}

$session = $this->_getSession();

if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost(‘login’);
$context = $this->getRequest()->getPost(‘context’);

if (!empty($login[‘username’]) && !empty($login[‘password’])) {

try {
$session->login($login[‘username’], $login[‘password’]);

if (!empty($context)) {
echo $context;
}

if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
}
} catch (Mage_Core_Exception $e) {
if (!empty($login[‘isajax’])) {

return;
}
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$value = $this->_getHelper(‘customer’)->getEmailConfirmationUrl($login[‘username’]);
$message = $this->_getHelper(‘customer’)->__(‘This account is not confirmed. <a href=”%s”>Click here</a> to resend confirmation email.’, $value);
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$session->addError($message);
$session->setUsername($login[‘username’]);
} catch (Exception $e) {
//Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
} else {
$session->addError($this->__(‘Login and password are required.’));
}
}
if (!empty($login[‘isajax’])) {
return;
} else {
$this->_loginPostRedirect();
}
}

I need it to keep running but run a js code “$this->_loginPostRedirect();”

I managed to get the request this way.

jQuery.ajax({
url: “<?php echo $this->getUrl(‘customer/account/loginPost’) ?>”,
type: “POST”,
success: function(data) {
if (data) {

}

}

});

The problem is that it returns the checkout page, and I need to put a parameter inside the if that really confirms that it is the checkout login, and run only on login, not on page refresh.

Leave a Reply

Your email address will not be published.