Solved

magento programatically registering a user

Posted on 2013-01-16
2
1,571 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where can I find... 1 31
write screen output to text file 21 47
Select record with the most recent date 14 56
Two wordpress questions 3 45
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn about the eCommerce marketing trends for the year ahead.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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