1

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()));
            }

         }

    }

}
4

1 に答える 1

2

Its a typo in your code

This line must be

       $address->setCountryId('US');

instead of

       $address->setCountryId('United States');

You should use the country id instead of name and country ID for the united states is US.. So use it and set it right.

于 2013-03-05T05:20:12.287 に答える