Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1687
  • Last Modified:

magento programatically registering a user

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
Th0R
Asked:
Th0R
1 Solution
 
Lukasz ChmielewskiCommented:
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
 
Th0RAuthor Commented:
know what? that worked !
thnx a mil
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now