Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

magento programatically registering a user

Posted on 2013-01-16
2
Medium Priority
?
1,668 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article was initially published on Monitis Blog, you can read it here . When it comes to deciding which approach to website performance monitoring is best for your business, unfortunately, like so many options in life . . . it depends. In th…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

705 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