2

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 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.

4

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:

<?php
namespace Craft;

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

$craft->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 '<b>Errors</b><br/>';
    foreach ($errors as $error) {
        echo 'Error: '.$error;
        echo '<br>';
    }
};

$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.

|improve this answer|||||
  • 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 Jun 28 '17 at 9:40
  • 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 Jun 28 '17 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.