Skip to content

Instantly share code, notes, and snippets.

@angelo510
Created January 19, 2017 16:55
Show Gist options
  • Save angelo510/9b3df4d6913148369476b89710b1574b to your computer and use it in GitHub Desktop.
Save angelo510/9b3df4d6913148369476b89710b1574b to your computer and use it in GitHub Desktop.
Magento Customer
<?php
$countryCollection = Mage::getModel('directory/country_api')->items();
function getCountryId($country_name){
global $countryCollection;
$country_name = trim($country_name);
foreach ($countryCollection as $country) {
if (strtolower($country['name']) == strtolower($country_name)){
return $country['country_id'];
}elseif (strtolower($country['country_id']) == strtolower($country_name)) {
return $country['country_id'];
}elseif (strtolower($country['iso2_code']) == strtolower($country_name)) {
return $country['country_id'];
}elseif (strtolower($country['iso3_code']) == strtolower($country_name)) {
return $country['country_id'];
}
}
return false;
}
function getRegionCollection($countryCode){
$regionCollection = Mage::getModel('directory/region_api')->items($countryCode);
return $regionCollection;
}
function createCustomer($customerOb){
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customerOb->email);
if(!$customer->getId()) {
$customer->setEmail($customerOb->email);
$customer->setFirstname($customerOb->fname);
$customer->setLastname($customerOb->lname);
$customer->setPassword($customer->generatePassword(10));
$customer->setData('group_id', 4);
}
try{
$customer->save();
$customer->setConfirmation(null);
$customer->save();
}catch(Exception $ex){
}
if(property_exists($customerOb, 'country')){
$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId());
$address->firstname = $customerOb->fname;
$address->lastname = $customerOb->lname;
$address->street = array(
'0' => '1 Imported Order'
);
$address->city = 'Imported';
$address->postcode = '11111';
$address->country_id = 'US';
$country_id = getCountryId($customerOb->country);
if ($country_id){
$address->country_id = $country_id;
}
$address->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try{
$address->save();
}catch(Exception $ex){
}
}else{
$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId());
$address->firstname = $customerOb->fname;
$address->lastname = $customerOb->lname;
$address->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try{
$address->save();
}catch(Exception $ex){
}
}
}
function addAddressToCustomer($addArray){
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($addArray['Email']);
foreach ($customer->getAddresses() as $address) {
//var_dump($address->debug());
$address->delete();
}
$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId());
$address->firstname = $addArray['Name (First)'];
$address->lastname = $addArray['Name (Last)'];
$address->street = array(
'0' => $addArray['Shipping Address Information (Street Address)'],
'1' => $addArray['Shipping Address Information (Address Line 2)'],
);
$address->city = $addArray['Shipping Address Information (City)'];
$address->postcode = $addArray['Post Id'];
$address->country_id = getCountryId($addArray['Shipping Address Information (Country)']);
$address->phone = $addArray['Phone'];
$address->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try{
$address->save();
}catch(Exception $ex){
var_dump($ex);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment