Created
May 11, 2021 10:23
-
-
Save jjmontalban/df8e79c6f582c45f444f33d39ab1f915 to your computer and use it in GitHub Desktop.
Add custom register fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @snippet Añadir campos al registro | |
* @author https://www.cloudways.com/blog/add-woocommerce-registration-form-fields/ | |
*/ | |
// 1. Add new register fields for WooCommerce registration. | |
function wooc_extra_register_fields() {?> | |
<p class="form-row form-row-first"> | |
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-last"> | |
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-last"> | |
<label for="reg_billing_company"><?php _e( 'Empresa (Opcional)', 'woocommerce' ); ?></label> | |
<input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-last"> | |
<label for="reg_billing_cif"><?php _e( 'CIF/NIF (No incluya espacios ni guiones) ', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_cif" id="reg_billing_cif" value="<?php if ( ! empty( $_POST['billing_cif'] ) ) esc_attr_e( $_POST['billing_cif'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="reg_billing_phone2"><?php _e( 'Teléfono 2', 'woocommerce' ); ?></label> | |
<input type="text" class="input-text" name="billing_phone2" id="reg_billing_phone2" value="<?php if ( ! empty( $_POST['billing_phone2'] ) ) esc_attr_e( $_POST['billing_phone2'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="reg_billing_address_1"><?php _e( 'Address', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php esc_attr_e( $_POST['billing_address_1'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php esc_attr_e( $_POST['billing_city'] ); ?>" /> | |
</p> | |
<p class="form-row form-row-wide"> | |
<label for="reg_billing_postcode"><?php _e( 'Postal Code', 'woocommerce' ); ?><span class="required">*</span></label> | |
<input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php esc_attr_e( $_POST['billing_postcode'] ); ?>" /> | |
</p> | |
<div class="clear"></div> | |
<?php | |
//Dropdowns especiales | |
wp_enqueue_script( 'wc-country-select' ); | |
woocommerce_form_field( 'billing_country', array( | |
'type' => 'country', | |
'class' => array('chzn-drop'), | |
'label' => __('Country'), | |
'placeholder' => __('Choose your country.'), | |
'required' => true, | |
'clear' => true | |
)); | |
wp_enqueue_script( 'wc-state-select' ); | |
woocommerce_form_field( 'billing_state', array( | |
'type' => 'state', | |
'class' => array('chzn-drop'), | |
'label' => __('Provincia'), | |
'placeholder' => __('Escoge tu provincia.'), | |
'required' => true, | |
'clear' => true | |
)); | |
} | |
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' ); | |
//2. Validacion de los campos | |
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) { | |
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) { | |
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: Nombre es requerido', 'woocommerce' ) ); | |
} | |
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) { | |
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Apellido es requerido', 'woocommerce' ) ); | |
} | |
if ( isset( $_POST['billing_cif'] ) && empty( $_POST['billing_cif'] ) ) { | |
$validation_errors->add( 'billing_cif_error', __( '<strong>Error</strong>: CIF es requerido', 'woocommerce' ) ); | |
} | |
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) { | |
$validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Telefono es requerido', 'woocommerce' ) ); | |
} | |
if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) { | |
$validation_errors->add( 'billing_address_1_error', __( '<strong>Error</strong>: Dirección es requerido', 'woocommerce' ) ); | |
} | |
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) { | |
$validation_errors->add( 'billing_city_error', __( '<strong>Error</strong>: Ciudad es requerido', 'woocommerce' ) ); | |
} | |
if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) { | |
$validation_errors->add( 'billing_postcode_error', __( '<strong>Error</strong>: Código postal es requerido', 'woocommerce' ) ); | |
} | |
return $validation_errors; | |
} | |
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 ); | |
//3. Insertar datos en la database | |
function wooc_save_extra_register_fields( $customer_id ) { | |
if ( isset( $_POST['billing_phone'] ) ) { | |
// Phone input field which is used in WooCommerce | |
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) ); | |
} | |
if ( isset( $_POST['billing_phone2'] ) ) { | |
// Phone2 input field which is used in WooCommerce | |
update_user_meta( $customer_id, 'billing_phone2', sanitize_text_field( $_POST['billing_phone2'] ) ); | |
} | |
if ( isset( $_POST['billing_first_name'] ) ) { | |
//First name field which is by default | |
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); | |
// First name field which is used in WooCommerce | |
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); | |
} | |
if ( isset( $_POST['billing_last_name'] ) ) { | |
// Last name field which is by default | |
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); | |
// Last name field which is used in WooCommerce | |
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); | |
} | |
if ( isset( $_POST['billing_company'] ) ) { | |
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) ); | |
} | |
if ( isset( $_POST['billing_cif'] ) ) { | |
update_user_meta( $customer_id, 'billing_cif', sanitize_text_field( $_POST['billing_cif'] ) ); | |
} | |
if ( isset( $_POST['billing_address_1'] ) ) { | |
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) ); | |
} | |
if ( isset( $_POST['billing_city'] ) ) { | |
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) ); | |
} | |
if ( isset( $_POST['billing_postcode'] ) ) { | |
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) ); | |
} | |
if ( isset( $_POST['billing_country'] ) ) { | |
update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) ); | |
} | |
if ( isset( $_POST['billing_state'] ) ) { | |
update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) ); | |
} | |
} | |
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