In WooCommerce have created an account registration page as in the following code:
<?php
/**
* Add new register fields for WooCommerce registration.
*/
function wooc_extra_register_fields() {
?>
<?php // field ?>
<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>
<?php // endfield ?>
<?php // field ?>
<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>
<?php // endfield ?>
<div class="clear"></div>
<?php // field ?>
<p class="form-row form-row-wide">
<label for="reg_Facebook"><?php _e( 'Facebook', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_Facebook" id="reg_Facebook" value="" placeholder="input Facebook url" />
</p>
<?php // endfield ?>
<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_whatsapp_phone"><?php _e( 'whatsapp', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="whatsapp_phone" id="reg_whatsapp_phone" value="" />
</p>
<?php
$field = [
'type' => 'country',
'label' => 'Country',
'required' => 1,
'class' => ['address-field']
];
woocommerce_form_field( 'billing_country', $field, '' );
$field = [
'type' => 'state',
'label' => 'State',
'required' => 1,
'class' => ['address-field'],
'validate' => ['state']
];
woocommerce_form_field( 'billing_state', $field, '' );
$handle = 'wc-country-select';
wp_enqueue_script($handle, get_site_url().'/wp-content/plugins/woocommerce/assets/js/frontend/country-select.min.js', array('jquery'), true);
?>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* Save the extra register fields.
*
* @param int $customer_id Current customer ID.
*/
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_country'] ) ) {
// WooCommerce billing country
update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
}
if ( isset( $_POST['billing_state'] ) ) {
// WooCommerce billing_state
update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
}
update_user_meta( $customer_id, 'billing_Facebook', sanitize_text_field( $_POST['billing_Facebook'] ) );
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
But I was not able to save the new fields such as (Facebook and WhatsApp account) to the member's (users.php) account page, which I would like them to appear as fields on the order registration page (as checkout fields). how fix it?
How to save the custom woocommerce field in the database and fields and put it as a fields in checkout page?
The following will optimize and compact your code, adding field validation and allowing to save all fields values as user meta data:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
To display "Facebook" and "WhatsApp" fields in My account edit billing address use the following:
To add "Facebook" and "WhatsApp" fields to Admin user billing section use the following: