Skip to content

Instantly share code, notes, and snippets.

@tozbey
Created October 11, 2020 21:41
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 tozbey/f13802ca0e29bd7b7e8fe00b4dea3f28 to your computer and use it in GitHub Desktop.
Save tozbey/f13802ca0e29bd7b7e8fe00b4dea3f28 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WooCommerce Registration Fields
* Description: My Custom registration fields.
* Version: 1.0
* Author: T
*/
/* Extra Fields in WooCommerce Registration */
/**
* Add new register fields for WooCommerce registration.
* Credit: https://stackoverflow.com/a/49173586
* @return string Register fields HTML.
*/
// Add extra registration fields
add_action( 'woocommerce_register_form_start', 'woo_add_extra_registration_fields', 20 );
function woo_add_extra_registration_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-wide">
<label for="reg_billing_company"><?php _e( 'Company', '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-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 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( 'Address', '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( 'Postcode', '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( 'Town / City', '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>
<div class="clear"></div>
<?php
}
// Validate require additional registration fields
add_action( 'woocommerce_register_post', 'woo_extra_registration_fields_validation', 20, 3 );
function woo_extra_registration_fields_validation( $username, $email, $validation_errors ) {
$domain = 'woocommerce';
$error = '<strong>' . __( 'Error', $domain ) . '</strong>: ';
// Billing First name field
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) )
$validation_errors->add( 'billing_first_name_error', $error . __( 'First name is required!', $domain ) );
// Billing Last name field
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) )
$validation_errors->add( 'billing_last_name_error', $error . __( 'Last name is required!.', $domain ) );
// Billing Phone field
if ( isset($_POST['billing_phone']) && empty( $_POST['billing_phone'] ) )
$validation_errors->add( 'billing_phone_error', $error . __( 'Phone is required!.', $domain ) );
// Billing Adress 1 Field
if ( isset($_POST['billing_address_1']) && empty( $_POST['billing_address_1'] ) )
$validation_errors->add( 'billing_address_1_error', $error . __( 'Address is required!.', $domain ) );
// Billing Postcode Field
if ( isset($_POST['billing_phone']) && empty( $_POST['billing_phone'] ) )
$validation_errors->add( 'billing_postcode_error', $error . __( 'Postcode is required!.', $domain ) );
// Billing City Field
if ( isset($_POST['billing_city']) && empty( $_POST['billing_city'] ) )
$validation_errors->add( 'billing_city_error', $error . __( 'City is required!.', $domain ) );
return $validation_errors;
}
// Save extra registration fields data
add_action('woocommerce_created_customer', 'woo_save_extra_registration_fields_data', 20, 1 );
function woo_save_extra_registration_fields_data( $customer_id ) {
// Billing First name field
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
// Billing Last name field
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
// Billing Company field
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
// Billing Phone Field
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'phone', sanitize_text_field( $_POST['billing_phone'] ) );
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
// Billing Adress 1 Field
if ( isset( $_POST['billing_address_1'] ) ) {
update_user_meta( $customer_id, 'address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
}
// Billing City Field
if ( isset( $_POST['billing_city'] ) ) {
update_user_meta( $customer_id, 'city', sanitize_text_field( $_POST['billing_city'] ) );
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
// Billing Postcode Field
if ( isset( $_POST['billing_postcode'] ) ) {
update_user_meta( $customer_id, 'postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment