Solved

magento programatically registering a user

Posted on 2013-01-16
2
1,535 Views
Last Modified: 2016-03-02
Hello

I have a page outside my magento directory. I can use my magento code in these pages.
But i have a problem when registering a new customer. It works, but it does not return an error when the customer already exists.

<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = '/www/htdocs/app/Mage.php';

if (!file_exists($mageFilename)) {
    if (is_dir('downloader')) {
        header("Location: downloader");
    } else {
        echo $mageFilename." was not found";
    }
    exit;
}
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);

Mage::app('default');

$customer_email = 'test331299@testemail.com';  // email adress that will pass by the questionaire 
$customer_fname = 'test_firstname';      // we can set a tempory firstname here 
$customer_lname = 'test_lastname';       // we can set a tempory lastname here 
$passwordLength = 10;                    // the lenght of autogenerated password

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);
/*
* Check if the email exist on the system.
* If YES,  it will not create a user account. 
*/

if(!$customer->getId()) {
    $customer->setEmail($customer_email);
    $customer->setFirstname($customer_fname);
    $customer->setLastname($customer_lname);
    $customer->setPassword($customer->generatePassword($passwordLength));
}
try {
    $customer->save();
    $customer->setConfirmation(null);
    $customer->save();
    //Make a "login" of new customer
    //Mage::getSingleton('customer/session')->loginById($customer->getId());
		echo 'user added';
}
catch (Exception $ex) {
   echo 'error';
}
?>

Open in new window


I have found 3 or 4 sites/forums where they use a methode like this. But all of them give the same problem. So i'm a bit lost.

When i execute this php file, a user is created correctly. But when i execute it again with the same email adres. It gives no error and gives me the "echo 'user added';" msg.
But ofcours the user is not recreated in magento.

I just want it to echo me that the user already exists

These are the ones i tried to use:
site1
site2
site3
0
Comment
Question by:Th0R
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 38783588
You could move the try into if

if(!$customer->getId()) {
    $customer->setEmail($customer_email);
    $customer->setFirstname($customer_fname);
    $customer->setLastname($customer_lname);
    $customer->setPassword($customer->generatePassword($passwordLength));

try {
    $customer->save();
    $customer->setConfirmation(null);
    $customer->save();
    //Make a "login" of new customer
    //Mage::getSingleton('customer/session')->loginById($customer->getId());
            echo 'user added';
}
catch (Exception $ex) {
   echo 'error';
}

}else{
 echo 'user already exists';
}
0
 

Author Closing Comment

by:Th0R
ID: 38784449
know what? that worked !
thnx a mil
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now