• Resolved Screenload

    (@screenload)


    Hey, great plugin, works like a charm!

    I just need to add new addresses directly via PHP, how can I do that?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Hall

    (@hallme)

    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.
    Thread Starter Screenload

    (@screenload)

    Thanks for the code, it helped me figuring out how the meta is saved.
    So basically you safe the extra adresses as shippingN_company and so on, but this is not working for me.

    The entries are in the database, but not displayed in the user backend.
    Could there be another counter in the database that needs updating or so?

    Also, is it possible to save the adresses without first / last name?
    Tried with and without but nothing worked…

    Thank you very much in advance!

    Plugin Author Hall

    (@hallme)

    You need the wc_address_book meta which is basically an array of the address names in the address book.

    It is the last line in my example above.

    All of the fields are optional so you don’t need first/last name.

    Thread Starter Screenload

    (@screenload)

    Ok that helped a bit, but there must be more to it.

    If I add addresses by hand, works perfect.

    If I add the same addresses to another account via PHP, nothing is shown in the backend. Everything in the database looks EXACTLY the same, tripple checked every char!

    Addresses are just not shown! I can even add another address by hand in the backend, they are stored in the database with the php imported ones but again not shown in the backend.

    Maybe I’ll go crazy but something is wrong there…

    Plugin Author Hall

    (@hallme)

    I’m not sure. I’ve tried adding them directly in the database that way and they work for me.

    The only other requirement that I can think of is you do need a default one in the database shipping_address_1 etc or else the address book doesn’t show up at all currently.

    Thread Starter Screenload

    (@screenload)

    Thank you soooo much, this acutally fixed it!
    My sample address had no first line so this was the issue!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add new adresses via PHP’ is closed to new replies.