Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Registration Custom Fields Saving
<?php
/** Save the extra register fields. */
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// WooCommerce billing first name.
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// WordPress default last name field.
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// WooCommerce billing last name.
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['subscribe_to_newsletter'] ) ) {
update_user_meta( $customer_id, 'subscribe_to_newsletter', sanitize_text_field( $_POST['subscribe_to_newsletter'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
/** Assign user role */
function wooc_assign_user_role($customer_data){
if ( isset( $_POST['user_role'] ) ) {
$customer_data['role'] = sanitize_text_field($_POST['user_role']);
}
return $customer_data;
}
add_filter( 'woocommerce_new_customer_data', 'wooc_assign_user_role');
/** Set default display name */
function default_display_name($name) {
if ( isset( $_POST['billing_first_name'] ) ) {
$firstname = sanitize_text_field( $_POST['billing_first_name'] );
}
if ( isset( $_POST['billing_last_name'] ) ) {
$lastname = sanitize_text_field( $_POST['billing_last_name'] );
}
$name = $firstname . ' ' . $lastname;
return $name;
}
add_filter('pre_user_display_name','default_display_name');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment