Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from nomanur/register.php
Created December 16, 2024 16:23
Show Gist options
  • Save dexit/767db99d4707f596817630a4b3c110d6 to your computer and use it in GitHub Desktop.
Save dexit/767db99d4707f596817630a4b3c110d6 to your computer and use it in GitHub Desktop.
woocommerce custom registration field on account
<?php
/**
* Plugin Name: WooCommerce Registration Fields
* Description: My Custom registration fields.
* Version: 1.0
* Author: Antonio Crespo
* License: GPL2
*/
/* Extra Fields in WooCommerce Registration */
/**
* Add new register fields for WooCommerce registration.
*
* @return string Register fields HTML.
*/
function wooc_extra_register_fields() {
?>
<p class="form-row form-row-wide">
<label for="reg_billing_first_name"><?php _e( 'Nombre', '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-wide">
<label for="reg_billing_last_name"><?php _e( 'Apellidos', '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-wide">
<label for="reg_billing_phone"><?php _e( 'Teléfono', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_address_1"><?php _e( 'Dirección', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_postcode"><?php _e( 'Código Postal / Zip', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_city"><?php _e( 'Localidad / Ciudad', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_state"><?php _e( 'Provincia', 'woocommerce' ); ?> <span class="required">*</span></label>
<select class="state_select" name="billing_state" id="reg_billing_state">
<option value="">Selecciona una província&hellip;</option><option value="C" >A Coru&ntilde;a</option><option value="VI" >Álava</option><option value="AB" >Albacete</option><option value="A" >Alicante</option><option value="AL" >Almería</option><option value="O" >Asturias</option><option value="AV" >Ávila</option><option value="BA" >Badajoz</option><option value="PM" >Baleares</option><option value="B" >Barcelona</option><option value="BU" >Burgos</option><option value="CC" >Cáceres</option><option value="CA" >Cádiz</option><option value="S" >Cantabria</option><option value="CS" >Castellón</option><option value="CE" >Ceuta</option><option value="CR" >Ciudad Real</option><option value="CO">Córdoba</option><option value="CU" >Cuenca</option><option value="GI" >Gerona</option><option value="GR" >Granada</option><option value="GU" >Guadalajara</option><option value="SS" >Guipúzcoa</option><option value="H" >Huelva</option><option value="HU" >Huesca</option><option value="J" >Jaén</option><option value="LO" >La Rioja</option><option value="GC" >Las Palmas</option><option value="LE" >León</option><option value="L" >Lérida</option><option value="LU" >Lugo</option><option value="M" >Madrid</option><option value="MA" >Málaga</option><option value="ML" >Melilla</option><option value="MU" >Murcia</option><option value="NA" >Navarra</option><option value="OR" >Orense</option><option value="P" >Palencia</option><option value="PO" >Pontevedra</option><option value="SA" >Salamanca</option><option value="TF" >Santa Cruz de Tenerife</option><option value="SG" >Segovia</option><option value="SE" >Sevilla</option><option value="SO" >Soria</option><option value="T" >Tarragona</option><option value="TE" >Teruel</option><option value="TO" >Toledo</option><option value="V" >Valencia</option><option value="VA" >Valladolid</option><option value="BI" >Vizcaya</option><option value="ZA" >Zamora</option><option value="Z" >Zaragoza</option>
</select>
</p>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* Validate the extra register fields.
*
* @param string $username Current username.
* @param string $email Current email.
* @param object $validation_errors WP_Error object.
*
* @return void
*/
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', __( 'Nombre es un campo requerido.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( 'Apellidos es un campo requerido.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
$validation_errors->add( 'billing_phone_error', __( 'Teléfono es un campo requerido.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
$validation_errors->add( 'billing_address_1_error', __( 'Dirección es un campo requerido.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {
$validation_errors->add( 'billing_postcode_error', __( 'Código postal / Zip es un campo requerido.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
$validation_errors->add( 'billing_city_error', __( 'Localidad / Ciudad es un campo requerido.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_state'] ) && empty( $_POST['billing_state'] ) ) {
$validation_errors->add( 'billing_state', __( 'Provincia es un campo requerido.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Save the extra register fields.
*
* @param int $customer_id Current customer ID.
*
* @return void
*/
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_address_1'] ) ) {
// WooCommerce billing address
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
}
if ( isset( $_POST['billing_postcode'] ) ) {
// WooCommerce billing postcode
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
}
if ( isset( $_POST['billing_city'] ) ) {
// WooCommerce billing city
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_state'] ) ) {
// WooCommerce 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