We actually have a script that we used to import some. Here is a version of it which I sanitized. Feel free to adapt it to your needs.
$user_id = 11;
$address_name = 'shipping';
$addresses = array(); // Fill with addresses to enter.
$address_names = get_user_meta( $user_id, 'wc_address_book', true );
if ( empty( $address_names ) ) {
$shipping_address = get_user_meta( $user_id, 'shipping_address_1', true );
// Return just a default shipping address if no other addresses are saved.
if ( empty( $shipping_address ) ) {
$address_names = array( 'shipping' );
}
// If we don't have a shipping address, just return an empty array.
$address_names = array();
}
$counter = count( $address_names ) + 1;
foreach ( $addresses as $address ) {
update_user_meta( $user_id, $address_name . $counter . '_first_name', $address['first_name'] );
update_user_meta( $user_id, $address_name . $counter . '_last_name', $address['last_name'] );
update_user_meta( $user_id, $address_name . $counter . '_company', $address['company'] );
update_user_meta( $user_id, $address_name . $counter . '_country', $address['country'] );
update_user_meta( $user_id, $address_name . $counter . '_address_1', $address['address_1'] );
update_user_meta( $user_id, $address_name . $counter . '_address_2', $address['address_2'] );
update_user_meta( $user_id, $address_name . $counter . '_city', $address['city'] );
update_user_meta( $user_id, $address_name . $counter . '_state', $address['state'] );
update_user_meta( $user_id, $address_name . $counter . '_postcode', $address['postcode'] );
$address_names[] = $address_name . $counter;
$counter++;
}
update_user_meta( $user_id, 'wc_address_book', $address_names );
-
This reply was modified 5 years, 7 months ago by Hall.