How To Programmatically Update User Billing Information In WooCommerce
The other day, I was importing a large CSV file of users for a client that was generated from a different e-commerce system. About 61k rows of information, which is no problem for Woocommerce & WordPress but it was a daunting task for me. It turned out great and there were almost no issues.
I’m going to have a few article that go over the process and detail the finer parts of each step, like this one. They’ll be about the parts of coding the import that weren’t exactly spelled out and I had to figure out on my own.
This is for anyone who would need to programmatically add a user’s billing address and other information in WooCommerce. It will also include how to update their name.
This method would work for all user information, but I wanted to list all the billing related meta keys. If you aren’t aware, WordPress and WooCommerce store meta data in a key / value pair system in the database. If you don’t know the key names, you won’t be able to set the right keys and then WooCommerce won’t know what to do with the data. So, here’s the code with the key names:
$billing_data = array( 'first_name' => $fname, 'last_name' => $lname, 'billing_city' => $city, 'billing_postcode' => $zip, 'billing_email' => $address_email, 'billing_phone' => $phone, 'billing_company' => $company, 'billing_address_1' => $address1, 'billing_address_2' => $address2, 'billing_country' => $country, 'billing_state' => $state, 'billing_first_name' => $fname, 'billing_last_name' => $lname ); foreach ($billing_data as $billing_meta_key => $billing_meta_value ) { update_user_meta( $user_id, $billing_meta_key, $billing_meta_value ); }
I know! It’s pretty straight forward in terms of naming conventions, but you never know when they might throw a curveball.