<p>Addresses are not Elements, so most import plugins will no support them as an import type.</p>

<p>You will need to create a plugin or script that imports the addresses into the commerce_addresses table. You could do this with a SQL query, but you could also use the Commerce address service like the example below.</p>

<p>Simply create an import.php file and place it in your public directory:</p>

<pre><code>&lt;?php
namespace Craft;

$craft = require '../craft/app/bootstrap.php';

$craft-&gt;plugins-&gt;loadPlugins();

$addressModel = new Commerce_AddressModel();

$addressModel-&gt;attention = '';
$addressModel-&gt;title = '';
$addressModel-&gt;firstName = 'John';
$addressModel-&gt;lastName = 'Doe';
$addressModel-&gt;address1 = '';
$addressModel-&gt;address2 = '';
$addressModel-&gt;city = '';
$addressModel-&gt;zipCode = '';
$addressModel-&gt;phone = '';
$addressModel-&gt;alternativePhone = '';
$addressModel-&gt;businessName = '';
$addressModel-&gt;businessTaxId = '';
$addressModel-&gt;businessId = '';
$addressModel-&gt;countryId = 1;
$addressModel-&gt;stateValue = 1; // can be (int) stateId or (string) state name

if(craft()-&gt;commerce_addresses-&gt;saveAddress($addressModel)){
    echo "Address Saved";
}else{
    $errors = $addressModel-&gt;getAllErrors();
    echo '&lt;b&gt;Errors&lt;/b&gt;&lt;br/&gt;';
    foreach ($errors as $error) {
        echo 'Error: '.$error;
        echo '&lt;br&gt;';
    }
};

$craft-&gt;end();
</code></pre>

<p>The above code will insert one Address.</p>

<p>You will then want to assign this address to a customer/user, so you will need to do the following:</p>

<p>1) Create a customer record in the <code>commerce_customers</code> table with the <code>userId</code> and <code>email</code> values set to that of the user.</p>

<p>2) Insert a record into the <code>commerce_customers_addresses</code> table with the <code>customerId</code> and <code>addressId</code> for each of the addresses belonging to that customer.</p>

<p>Steps above can be done directly as SQL inserts into the tables mentioned.</p>

<p>This will make the address available for selection on checkout.</p>

CC BY-SA 3.0
2017-06-28T09:40:55.240Z
Thanks Luke, that's extremely helpful. It doesn't look like the saveAddress function returns the id of the address it is saving, but I'm sure I can figure out a way to get that.
Jai Redden
CC BY-SA 3.0
2017-06-28T10:11:21.593Z
Ah, ignore the above comment, the id is set in $addressModel->id during the saveAddress call. I was overlooking PHP's object handling.
Jai Redden
CC BY-SA 3.0
2017-06-28T01:27:57.330Z
2017-06-28T01:27:57.330Z
Luke Holder
<p>I have an existing database full of users with their addresses. I'd like to import this into Craft (Commerce), and have their addresses saved as their billing address.</p>

<p>The user import part is fine, I can use <a href="https://github.com/boboldehampsink/import" rel="nofollow noreferrer">boboldehampsinks plugin</a> to get the users in, and I can add custom fields to temporarily hold the address information until I can add it as their billing address.</p>

<p>Can anyone suggest a good way to go about this? The documentation for Craft Commerce doesn't seem to be as complete as for Craft, and I can't see a way to set a users billing address.</p>

<p>Thanks.</p>

<plugin-craftcommerce><users><addresses>
How to import user addresses?
# How to import user addresses?

(Asked by: username_0 on 2017-06-27)

I have an existing database full of users with their addresses. I'd like to import this into Craft (Commerce), and have their addresses saved as their billing address.

The user import part is fine, I can use [boboldehampsinks plugin](https://github.com/boboldehampsink/import) to get the users in, and I can add custom fields to temporarily hold the address information until I can add it as their billing address.

Can anyone suggest a good way to go about this? The documentation for Craft Commerce doesn't seem to be as complete as for Craft, and I can't see a way to set a users billing address.

Thanks.

**Tags:** plugin-craftcommerce, users, addresses

---

## 1 answers

### Answer by username_1 on 2017-06-28. Score: 4 | Accepted answer

Addresses are not Elements, so most import plugins will no support them as an import type.
You will need to create a plugin or script that imports the addresses into the commerce_addresses table. You could do this with a SQL query, but you could also use the Commerce address service like the example below.
Simply create an import.php file and place it in your public directory:
plugins->loadPlugins();

$addressModel = new Commerce_AddressModel();

$addressModel->attention = '';
$addressModel->title = '';
$addressModel->firstName = 'John';
$addressModel->lastName = 'Doe';
$addressModel->address1 = '';
$addressModel->address2 = '';
$addressModel->city = '';
$addressModel->zipCode = '';
$addressModel->phone = '';
$addressModel->alternativePhone = '';
$addressModel->businessName = '';
$addressModel->businessTaxId = '';
$addressModel->businessId = '';
$addressModel->countryId = 1;
$addressModel->stateValue = 1; // can be (int) stateId or (string) state name

if(craft()->commerce_addresses->saveAddress($addressModel)){
    echo "Address Saved";
}else{
    $errors = $addressModel->getAllErrors();
    echo 'Errors';
    foreach ($errors as $error) {
        echo 'Error: '.$error;
        echo '';
    }
};

$craft->end();

The above code will insert one Address.
You will then want to assign this address to a customer/user, so you will need to do the following:
1) Create a customer record in the commerce_customers table with the userId and email values set to that of the user.
2) Insert a record into the commerce_customers_addresses table with the customerId and addressId for each of the addresses belonging to that customer.
Steps above can be done directly as SQL inserts into the tables mentioned.
This will make the address available for selection on checkout.

> **Comments:**
> **username_0**: Thanks Luke, that's extremely helpful. It doesn't look like the saveAddress function returns the id of the address it is saving, but I'm sure I can figure out a way to get that.  
> **username_0**: Ah, ignore the above comment, the id is set in $addressModel->id during the saveAddress call. I was overlooking PHP's object handling.  

---

