eristoddle
1/16/2013 - 5:07 AM

Random Magento Customer Functions

Random Magento Customer Functions

<?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();
		  //TODO: Uncomment when live sends customer password
		  //$customer->sendNewAccountEmail();
		}catch(Exception $ex){
		 
		}

    //In order to create orders, we need an address - faking it
		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);
    }
  }
?>