Finding long addresses in Magento

A recent, and not particularly well-published, change in the policies of a client’s payment gateway restricted long addresses and resulted in a large number of orders failing midway through the checkout. We won’t name and shame the payment gateway in question, but in short they decided to impose a 35 character limit on each of the address line fields and failed any transactions with more than that number of characters. No feedback was given when failing the transaction – the customer was just returned to the cart page – which was monumentally unhelpful.

This meant two things – new addresses put in whilst the customer was in the process of checking out would cause the checkout to fail if either of the address lines was over 35 characters, as would addresses selected from the address book of existing customers where the address was created historically with more than 35 characters. So existing customers who’d previously had no problems ordering would suddenly find themselves bouncing back to the cart page on checkout.

Dealing with the first issue was relatively simple – just add a maxlength=”35″ to all the relevant input boxes. Bear in mind that this is not just on the checkout, but also when the customer adds a new address to their address book or edits an existing address (customer/form/address.phtml and customer/address/edit.phtml).

The second required a little more thought – with 4500 or so existing addresses, clearly doing it manually wasn’t an option, so we wrote a script to look through all those addresses, and pull out a separate list of those customers who had addresses in their address books whose Company Name or Address Line 1 or Address Line 2 had more than 35 characters so that they could be adjusted manually.

The script is as follows (and, as ever, is at the foot of this post ready to be downloaded). Start off by hooking into Magento (the script assumes that it’s in the web root of the installation, so adjust the first line as appropriate) :

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Then load all the addresses from the customer address collection into a variable :

$allAddresses = Mage::getResourceModel('customer/address_collection');

Then prepare the CSV file we’re going to write to by creating it and writing the header row to it :

$fp = fopen('var/outgoing/addresses.csv', 'w');
$headerRow = array('Customer ID', 'First name', 'Last name', 'Last order date');
fputcsv($fp, $headerRow);

Then loop through all the addresses in turn and load the full address into $fullDetails.

foreach ($allAddresses as $oneAddress) { 
	$fullDetails = Mage::getModel('customer/address')->load($oneAddress->getEntityId());

If any of the address lines or the company field in the full address have a length greater than 35 characters, then start collating the information needed for the CSV – first by getting the customer ID, first name and lastname :

	if ( (strlen($fullDetails->getCompany()) > 35) || (strlen($fullDetails->getStreet(1)) > 35) || (strlen($fullDetails->getStreet(2)) > 35)|| (strlen($fullDetails->getStreet(3)) > 35) ) {
		$customerID =  $fullDetails->getParentId();
		$firstname = $fullDetails->getFirstname();
		$lastname = $fullDetails->getLastname();

Then use the customer ID to load all the orders that that customer has placed, order it by the created date (descending), and pull the first item out to get the date of their last order :

		$orderCollection = Mage::getModel('sales/order')->getCollection()->addFilter('customer_id', $customerID)->setOrder('created_at', Varien_Data_Collection_Db::SORT_ORDER_DESC);
		$newestOrder = $orderCollection->getFirstItem();
		$latestDate = $newestOrder-> getData('created_at');

Finally take those details and write them to the CSV :

	$fields = array($customerID, $firstname, $lastname, $latestDate);
	fputcsv($fp, $fields);
	}
}
?>

And that’s it. Hit the script in your browser or a shell, and the CSV should be created at var/outgoing/addresses.csv ready to be downloaded.

Note that the script may output a customer more than once, as it will output the customer once for each address that they have which has fields in it longer than 35 characters, so the presence of a customer multiple times just indicates that there are multiple addresses for them which need to be looked at.