user3747074
user3747074

Reputation: 63

Setting country for all customers in Magento

I just went through hours of meticulous work to transfer over 20k customers for an access db into Magento 1.7.

Now the issue i am having is the country is not being recognized or picked up by magento for all the address. I do not want to go through another import/export process.

I just need a simple SQL script to set all the COUNTRY fields (in all address related tables) to "US" or "United States" (whichever format Magento uses)

Any help would be greatly appreciated.

Upvotes: 1

Views: 1509

Answers (2)

Kevin Sadler
Kevin Sadler

Reputation: 2456

I had a similar problem to the OP. In my CSV import for customers I had the country name, not the 2 letter ISO code. The import did not complain. However when a customer tried to place an order not shipping method could be computed - this turned out to be because the country code was wrong.

I took the script @liyakat wrote in his excellent answer and I have made a number of enhancements that I would like to share:

  • It will fix the Shipping AND Billing address
  • If there is a value in the countryId it will do a lookup to find the code from that country name... but...
  • If the countryId already contains a two letter code it leaves it alone
  • It defaults to GB "United Kingdom" cause I am a British
  • It sets the time_limit to zero - no limit, I don't know if this affects command line PHP or not, but I wasn't going to risk it stopping half way.
  • It prints the email of the customer it is processing. It is quite slow, so this gives you an idea of the progress.

The getCountryId() code comes from this question on Stackoverflow


    ini_set('memory_limit','2048M');
    set_time_limit(0);
    error_reporting(E_ALL | E_STRICT);
    require 'app/Mage.php';
    $app = Mage::app('default'); 

    // Method to Get countryId from CountryName
    function getCountryId($countryName) {

    //Most will be here, so save a lookup...
    if ($countryName == "United Kingdom")
    {
        $countryId = "GB";
    }
    else
    {
        $countryId = '';
        $countryCollection = Mage::getModel('directory/country')->getCollection();
        foreach ($countryCollection as $country) {
        if ($countryName == $country->getName()) {
            $countryId = $country->getCountryId();
            break;
        }
        }
    }
    $countryCollection = null;
    return $countryId;
    }

    function fixAddress($customer, $address, $isBilling)
    {
        if ($isBilling)
        {
            $address
            ->setCustomerId($customer->getId())
            ->setIsDefaultBilling('1')
            ->setSaveInAddressBook('1') ;           
        }
        else {
            $address
            ->setCustomerId($customer->getId())
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1');
        }


        $address_arr = $address->getData();
        $save = false;

        if ( isset($address_arr['country_id']) ) {

            if (strlen($address_arr['country_id']) != 2) {
                $isoCountry = getCountryId($address_arr['country_id']); 
                $address->setCountryId($isoCountry);
                $save = true;
            }
        }
        else
        {
            $address->setCountryId('GB');
            $save = true;
        }


        if ($save) {
            try {
                $address->save();

            } catch(Exception $e) {
                error_log(json_encode($e->getMessage()));
            }
    }


    }



    $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');
    $default_billing_id = $customer->getDefaultBilling();
    $address->load($default_billing_id);

        echo $customer->getEmail() . ' ' . getCountryId($address->getCountryId()) . "
\n"; fixAddress($customer, $address, true); if ($default_shipping_id = $customer->getDefaultShipping()) { $address->load($default_shipping_id); fixAddress($customer, $address, true); } }

Upvotes: 1

liyakat
liyakat

Reputation: 11853

you can use below script to set US as default country on all customer

EDIT

ini_set('memory_limit','2048M');
error_reporting(E_ALL | E_STRICT);
require 'app/Mage.php';
$app = Mage::app('default');


$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('US');

            try {
                $address->save();

            } catch(Exception $e) {
                error_log(json_encode($e->getMessage()));
            }

         }

    }

}

hope this will sure help to you

Just let me know if i could help you more.

Upvotes: 1

Related Questions