ANyone have an example script for updating a customers default shipping + billing address? I need to loop through all customers, and set the country to the default "United States" in the event they do not have a country specified.
I tried the following, no luck. Any thoughts?
$customers = Mage::getResourceModel('customer/customer_collection');
foreach ($customers as $customer) {
// customer object
$customer = Mage::getModel('customer/customer')->load($customer->getId());
$address = Mage::getModel('customer/address');
if ($default_shipping_id = $customer->getDefaultShipping()) {
$address->load($default_shipping_id);
} else {
$address
->setCustomerId($customer->getId())
->setIsDefaultShipping('1')
->setSaveInAddressBook('1')
;
$address_arr = $address->getData();
// country
if ( !isset($address_arr['country_id']) ) {
$address->setCountryId('United States');
try {
$address->save();
fwrite(STDOUT, '++ COUNTRY UPDATED FOR ' . $customer->getId() . "\n");
} catch(Exception $e) {
error_log(json_encode($e->getMessage()));
}
}
}
}