Solved

magento programatically registering a user

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

739 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