Skip to content

Instantly share code, notes, and snippets.

@odirlon
Created February 28, 2017 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odirlon/670d89b2aad2be6bfebed8647bed8281 to your computer and use it in GitHub Desktop.
Save odirlon/670d89b2aad2be6bfebed8647bed8281 to your computer and use it in GitHub Desktop.
/*
* Validate the extra register fields.
*
* @param WP_Error $validation_errors Errors.
* @param string $username Current username.
* @param string $email Current email.
*
* @return WP_Error
*/
function wooc_validate_extra_register_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
$errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_cpf'] ) && empty( $_POST['billing_cpf'] ) ) {
$errors->add( 'billing_cpf_error', __( '<strong>Error</strong>: CPF is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['googleplus'] ) && empty( $_POST['googleplus'] ) ) {
$errors->add( 'googleplus_error', __( '<strong>Error</strong>: facebook is required!.', 'woocommerce' ) );
}
return $errors;
}
add_filter( 'woocommerce_registration_errors', 'wooc_validate_extra_register_fields', 10, 3 );
/*
SALVANDO
*/
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'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['billing_cpf'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'billing_cpf', sanitize_text_field( $_POST['billing_cpf'] ) );
}
if ( isset( $_POST['googleplus'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'googleplus', sanitize_text_field( $_POST['googleplus'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment