Skip to content

Instantly share code, notes, and snippets.

@anunay
Created December 3, 2013 05:52
Show Gist options
  • Save anunay/7764507 to your computer and use it in GitHub Desktop.
Save anunay/7764507 to your computer and use it in GitHub Desktop.
Magento: Create Customer & Subscription Programtically
<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = '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');
$fname = @$_POST['fullname'];
$fullname = explode(" ",$_POST['fullname']);
$customer_email = @$_POST['user_email']; // email adress that will pass by the questionaire
$customer_fname = $fullname['fname']; // we can set a tempory firstname here
$customer_lname = $fullname['lname']; // we can set a tempory lastname here
$password = @$_POST['user_password']; // 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.
*/
//print_r($customer);
//echo "Customer ID " . $customer->getId();
if(!$customer->getId()) {
//setting data such as email, firstname, lastname, and password
$customer->setEmail($customer_email);
$customer->setFirstname($customer_fname);
$customer->setLastname($customer_lname);
$customer->setPassword($password);
$customer->setIsSubscribed("yes");
$customer->setData( 'group_id', 1 );
try{
//the save the data and send the new account email.
$customer->setConfirmation(null);
$customer->sendNewAccountEmail();
$customer->save();
//create new subscriber without send an confirmation email
//Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($customer_email);
$subscriber = Mage::getModel('newsletter/subscriber')->subscribe($customer_email);
// load up the subscriber if possible
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customer_email);
if (!$subscriber->getId()
|| $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED
|| $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->setSubscriberEmail($customer_email);
$subscriber->setSubscriberConfirmCode($subscriber->RandomSequence());
}
$subscriber->setStoreId(Mage::app()->getStore()->getId());
$subscriber->setCustomerId($customer->getId());
try {
$subscriber->save();
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
header("Location: {URL_TO_REDIRECT}");
exit;
}
catch(Exception $ex){
echo $ex->getMessage();
exit;
Mage::getSingleton("core/session")->addError($ex->getMessage());
header("Location: {URL_TO_REDIRECT}");
exit;
}
}else{
$message = "Email address already exists.";
//Mage::getSingleton('customer/session')->addError($message);
//Mage::getSingleton("core/session")->addError($message);
//$url = urlencode("{$fname}&email={$customer_email}");
header("Location: /promos/signup-05.html?err=already_exists&fname={$fname}&email={$customer_email}");
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment