Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tegansnyder/7415555 to your computer and use it in GitHub Desktop.

Select an option

Save tegansnyder/7415555 to your computer and use it in GitHub Desktop.
An example of adding a new customer to Magento with a auto-generated password and custom welcome email.
<?php
// sending a custom password to customers
require_once('app/Mage.php');
umask(0);
Mage::app();
$store_id = Mage::app()->getStore()->getId();
$customer_email = 'some-customer@domain.com';
$customer_fname = 'John';
$customer_lname = 'Doe';
$passwordLength = 10;
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);
if (!$customer->getId()) {
$customer->setEmail($customer_email);
$customer->setFirstname($customer_fname);
$customer->setLastname($customer_lname);
$password = $customer->generatePassword($passwordLength);
$customer->setPassword($password);
try {
$customer->save();
$customer->setConfirmation(null);
$customer->save();
//$customer->sendNewAccountEmail();
$templateId = 10;
$sender = Array('name' => Mage::getStoreConfig('trans_email/ident_general/name'),
'email' => Mage::getStoreConfig('trans_email/ident_general/email'));
$store = Mage::app()->getStore();
$name = $customer_fname . ' ' . $customer_lname;
$vars = Array('customer_name' => $name,
'new_password' => $password);
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')->sendTransactional($templateId,
$sender,
$customer_email,
$name,
$vars,
$store->getId());
$translate->setTranslateInline(true);
} catch(Exception $e) {
echo $e->getMessage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment