IN WooCommerce, I have been able to add a custom fields in Edit Account page.
I have tried adding a 2nd custom field "Favorite Color 2" but I ca't get it working, There is something that I am doing wrong.
How I can make to add/save an additional custom field in Edit Account page?
// Add the custom field "favorite_color"
add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );
function add_favorite_color_to_edit_account_form() {
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr( $user->favorite_color ); ?>" />
</p>
<?php
}
// Save the custom field 'favorite_color'
add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
// For Favorite color
if( isset( $_POST['favorite_color'] ) )
update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );
// For Billing email (added related to your comment)
if( isset( $_POST['account_email'] ) )
update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
This can be done very easily Making some changes in your code this way:
// Add the custom field "favorite_color"
add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );
function add_favorite_color_to_edit_account_form() {
$user = wp_get_current_user();
// First Field
?>
<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr( $user->favorite_color ); ?>" />
</p>
<?php
// Second Field
?>
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
<label for="favorite_color"><?php _e( 'Favorite color 2', 'woocommerce' ); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color2" id="favorite_color2" value="<?php echo esc_attr( $user->favorite_color2 ); ?>" />
</p>
<div class="clear"></div>
<?php
}
// Save the custom field 'favorite_color'
add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
// For Favorite color
if( isset( $_POST['favorite_color'] ) )
update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );
// For Favorite color 2
if( isset( $_POST['favorite_color2'] ) )
update_user_meta( $user_id, 'favorite_color2', sanitize_text_field( $_POST['favorite_color2'] ) );
// For Billing email (added related to your comment)
if( isset( $_POST['account_email'] ) )
update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
Tested and works.
You will get this:
Use woocommerce_edit_account_form hook as the below example
add_action( 'woocommerce_edit_account_form', 'cssigniter_add_account_details' );
function cssigniter_add_account_details() {
woocommerce_form_field(
'billing_company',
array(
'type' => 'text',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Company Name'
),
get_user_meta( get_current_user_id(), 'billing_company', true ) // get the data
);
woocommerce_form_field(
'company_id',
array(
'type' => 'number',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Company ID'
),
get_user_meta( get_current_user_id(), 'company_id', true ) // get the data
);
}
// Save field value
add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
function misha_save_account_details( $user_id ) {
update_user_meta( $user_id, 'billing_company', wc_clean( $_POST[ 'phone_number' ] ) );
update_user_meta( $user_id, 'company_id', wc_clean( $_POST[ 'phone_number' ] ) );
}
// Make it required
add_filter( 'woocommerce_save_account_details_required_fields', 'misha_make_field_required' );
function misha_make_field_required( $required_fields ){
$required_fields[ 'billing_company' ] = 'Company Name';
$required_fields[ 'company_id' ] = 'Company ID';
return $required_fields;
}
Add the above code to you theme functions.php file.
Related
I have multiple fields in "My Account" page, so, I wanted to add additional field for Date of Birth.
I am using part of Add birthday field to my account and admin user page
The field shows up but it come in the bottom.
I wanted to add DOB field after my 2 existing fields, I tried using 'priority => 20,' but it doesn't work.
// Add field - my account
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'My Birth Date', 'woocommerce' ),
'placeholder' => __( 'Date of Birth', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
As can be seen in myaccount/form-edit-account.php template file the woocommerce_edit_account_form action hook is executed after the already existing fields are added.
So if you want to add the field between the existing fields you will have to edit the template file
This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-edit-account.php
You have to delete the code you currently use
Add this code between 2 existing fields (at the place where you want to add the new field) in the myaccount/form-edit-account.php template file
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_birthday"><?php esc_html_e( 'Birth day', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="date" class="woocommerce-Input woocommerce-Input--text input-text" name="account_birthday" id="account_birthday" value="<?php echo get_user_meta( get_current_user_id(), 'account_birthday', true ); ?>"/>
</p>
<div class="clear"></div>
For validation and saving then add the following code to functions.php (
or the way you prefer to add snippets)
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset( $_POST['account_birthday'] ) && empty( $_POST['account_birthday'] ) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset( $_POST['account_birthday'] ) && ! empty( $_POST['account_birthday'] ) ) {
update_user_meta( $user_id, 'account_birthday', sanitize_text_field( $_POST['account_birthday'] ) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
Below is my child theme functions.php :
<?php
function remove_image_zoom_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
}
add_action( 'wp', 'remove_image_zoom_support', 100 );
/*PROBLEM CODE BELOW*/
///////////////////////////////
// 1. ADD FIELDS
add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
function bbloomer_add_name_woo_account_registration() {
?>
<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>
<div class="clear"></div>
<?php
}
///////////////////////////////
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );
function bbloomer_validate_name_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' ) );
}
return $errors;
}
///////////////////////////////
// 3. SAVE FIELDS
add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
function bbloomer_save_name_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
}
?>
I tried to see if the child theme is working, so I added background color to style.css. It didn't work, but function remove_image_zoom_support works.
I am trying to customize 'My Account Register Form' in WooCommerce, so I referred to this site: https://businessbloomer.com/woocommerce-add-first-last-name-account-register-form/. For some reason, the register form looks just the same as before (The form only has e-mail field and password field as default).
Is there any class or something I should correct?
Thank you in advance.
I have seen similar questions but couldn't find a solution for me.
I am trying to add custom fields to WooCommerce registration form, specifically first and last name field. I have managed to create these fields but the information entered does not pass over to the Account Details page when the user has logged in. Other tutorials have mentioned validating the fields but I am not sure is it relevant to me or not. I am working on a Wordpress child theme.
Please visit to codepad .org to view the code.
I tried to paste the code here by using the code sample option but it doesn't work properly.
Hope I have explained myself clearly. If not please let me know and I will clarify.
I think you have overwrite woocommerce/templates/myaccount/form-login.php template, and by doing that you have managed to show the billing_first_name and billing_last_name but you forget to use woocommerce_created_customer hook which is needed to save those data into your database.
What I'll suggest is that you keep the template as it is and add those field through function.php
Here is the code to add custom fields in WooCommerce registration form:
/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
<label for="billing_first_name"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="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="billing_last_name"><?php _e('Last name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
Coming to your second part of your question on validation it is totally optional and depends on your business logic that what you want, in general most of the site has First Name and the Last Name required but again it totally depends upon you, If you don't want to validate this then remove <span class="required">*</span> from above code and skip this section.
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_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>: First name is required!', 'text_domain'));
}
if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
$validation_errors->add('billing_last_name_error', __('<strong>Error</strong>: Last name is required!.', 'text_domain'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
Now this is a main part and this what you was had missed, below code is needed to save the custom data:
/**
* To save WooCommerce registration form custom fields.
*/
function text_domain_woo_save_reg_form_fields($customer_id) {
//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']));
}
//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']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
All the above code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
The code is tested and fully functional.
Hope this helps!
<?php
/**
* Add new register fields for WooCommerce registration
* To add WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
function misha_add_register_form_field(){
woocommerce_form_field(
'billing_first_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'First name'
),
( isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : '' )
);
woocommerce_form_field(
'billing_last_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Last name'
),
( isset($_POST['billing_last_name']) ? $_POST['billing_last_name'] : '' )
);
woocommerce_form_field(
'billing_phone',
array(
'type' => 'tel',
'required' => true, // just adds an "*"
'label' => 'Phone'
),
( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' )
);
}
/**
* To validate WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
function misha_validate_fields( $username, $email, $errors ) {
if ( empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', 'First name is required!' );
}
if ( empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', 'Last name is required!' );
}
if ( empty( $_POST['billing_phone'] ) ) {
$errors->add( 'billing_phone_error', 'Phone is required!' );
}
}
/**
* To save WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
function misha_save_register_fields( $customer_id ){
//First name field
if ( isset( $_POST['billing_first_name'] ) ) {
//update_user_meta( $customer_id, 'country_to_visit', wc_clean( $_POST['country_to_visit'] ) );
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']));
}
//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']));
}
// WooCommerce billing phone
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
}
?>
Adding Woocommerce registration form custom fields using filter hook woocommerce_forms_field and saving data with nonce verification.
/**
* Adding more fields to Woocommerce Registration form and My account page
*/
function how_woocommerce_registration_form_fields() {
return apply_filters( 'woocommerce_forms_field', array(
'billing_company' => array(
'type' => 'text',
'label' => __( 'Company Name', ' how' ),
'required' => false,
),
'how_user_job_title' => array(
'type' => 'text',
'label' => __( 'Job Title', ' how' ),
'required' => false,
),
'how_user_industry' => array(
'type' => 'select',
'label' => __( 'Industry', 'how' ),
'options' => array(
'' => __( 'Select an Industry', 'how' ),
'Lending' => __( 'Lending', 'how' ),
'Real Estate' => __( 'Real Estate', 'how' ),
'Investment' => __( 'Investment', 'how' ),
'Servicing' => __( 'Servicing', 'how' ),
'Other' => __( 'Other', 'how' ),
'Mortgage Servicing' => __( 'Mortgage Servicing', 'how' ),
'Mortgage Lending' => __( 'Mortgage Lending', 'how' ),
)
)
) );
}
function how_woocommerce_edit_registration_form() {
$fields = how_woocommerce_registration_form_fields();
foreach ( $fields as $key => $field_args ) {
woocommerce_form_field( $key, $field_args );
}
}
add_action( 'woocommerce_register_form', 'how_woocommerce_edit_registration_form', 15 );
/**
* Save registration form custom fields
*/
function wooc_save_extra_register_fields( $customer_id ) {
if( wp_verify_nonce( sanitize_text_field( $_REQUEST['woocommerce-register-nonce'] ), 'woocommerce-register' ) ) {
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
update_user_meta( $customer_id, 'shipping_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['how_user_job_title'] ) ) {
update_user_meta( $customer_id, 'how_user_job_title', sanitize_text_field( $_POST['how_user_job_title'] ) );
}
if ( isset( $_POST['how_user_industry'] ) ) {
update_user_meta( $customer_id, 'how_user_industry', sanitize_text_field( $_POST['how_user_industry'] ) );
}
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
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>
<div class="clear"></div>
<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_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
<select name="country" >
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AU">Australia</option>
<option value="IL">Israel</option>
<option value="IT">Italy</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', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
$validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', '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['country'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'country', sanitize_text_field( $_POST['country'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
if ($_POST['country']=='IT') {
$current_user = wp_get_current_user();
$user_data = array(
'ID' => $current_user->ID,
'role' => 'New Role Here'
);
wp_update_user( $user_data );
}
I am developing plugin which will add extra field in my account page (User registration).
I will add first name, last name ,phone number and country.
But my main aim is to assign used role based on country like
if user has selected Italy then role should be Italy (I have already created this role in back end )
Could you please suggest me ,how to achieve it
Write the code for adding user role inside woocommerce_created_customer hook.
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['country'])) {
// WooCommerce billing phone
update_user_meta($customer_id, 'country', sanitize_text_field($_POST['country']));
// Assign user role
switch ($_POST['country']) {
case 'IT':
wp_update_user(array(
'ID' => $customer_id,
'role' => 'editor' // Update to desired role
));
break;
}
}
}
add_action('woocommerce_created_customer', 'wooc_save_extra_register_fields');
I have created two plugins:
To add 3 custom fields on register form
To add this 3 custom fields on checkout page and user dashboard panel.
I have successfuly added my fields in register form, checkout page and user dashboar panel but on checkout page I cannot populate them with the presaved values.
First plugin is for adding new fields in registration. The new fields are: 'billing_bulstat' ; 'billing_mol' and 'billing_adres'. They are succesfuly saved and displayed in user dashboard panel. The code is:
<?php
/**
* Plugin Name: WooCommerce Registration Fields in Luga.bg - Traders Store
* Plugin URI: http://www.luga.bg/traders
* Description: My Custom registration fields in Luga.bg - Traders Store
* Version: 2.0
* Author: Nikolay Grudev
* Author URI: http://www.luga.bg/traders
* License: GPL3
*/
/**
* Add new register fields for WooCommerce registration in Luga.bg - Traders Store.
*
* #return string Register fields HTML.
*/
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>
<div class="clear"></div>
<p class="form-row form-row-wide">
<label for="reg_billing_company"><?php _e( 'Име на фирмата', 'woocommerce' ); ?> <span
class="required">*</span></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>
<div class="clear"></div>
<p class="form-row form-row-first">
<label for="reg_billing_bulstat"><?php _e( 'ЕИК', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_bulstat" id="reg_billing_bulstat" value="<?php if ( ! empty(
$_POST['billing_bulstat'] ) ) esc_attr_e( $_POST['billing_bulstat'] ); ?>" />
</p>
<p class="form-row form-row-last">
<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 if ( ! empty(
$_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_mol"><?php _e( 'МОЛ (Материално отговорно лице)', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_mol" id="reg_billing_mol" value="<?php if ( ! empty( $_POST
['billing_mol'] ) ) esc_attr_e( $_POST['billing_mol'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_adres"><?php _e( 'Адрес за фактуриране', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_adres" id="reg_billing_adres" value="<?php if ( ! empty(
$_POST['billing_adres'] ) ) esc_attr_e( $_POST['billing_adres'] ); ?>" />
</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', __( '<strong>Грешка</strong>: Първото име е
задъжлително!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Грешка</strong>: Фамилията е
задължителна!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
$validation_errors->add( 'billing_company_error', __( '<strong>Грешка</strong>: Името на фирмата е
задължително!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_bulstat'] ) && empty( $_POST['billing_bulstat'] ) ) {
$validation_errors->add( 'billing_bulstat_error', __( '<strong>Грешка</strong>: ЕИК на фирмата е
задължителен!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
$validation_errors->add( 'billing_city_error', __( '<strong>Грешка</strong>: Градът е задължителен!.',
'woocommerce' ) );
}
if ( isset( $_POST['billing_mol'] ) && empty( $_POST['billing_mol'] ) ) {
$validation_errors->add( 'billing_mol_error', __( '<strong>Грешка</strong>: МОЛ е задължително!.',
'woocommerce' ) );
}
if ( isset( $_POST['billing_adres'] ) && empty( $_POST['billing_adres'] ) ) {
$validation_errors->add( 'billing_adres_error', __( '<strong>Грешка</strong>: Адресът за фактуриране е
задължителен!.', '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_company'] ) ) {
// WooCommerce company name
update_user_meta( $customer_id, 'company', sanitize_text_field( $_POST['billing_company'] ) );
// WooCommerce billing company name.
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['billing_bulstat'] ) ) {
// WooCommerce company bulstat
update_user_meta( $customer_id, 'bulstat', sanitize_text_field( $_POST['billing_bulstat'] ) );
// WooCommerce billing company bulstat
update_user_meta( $customer_id, 'billing_bulstat', sanitize_text_field( $_POST['billing_bulstat'] ) );
}
if ( isset( $_POST['billing_city'] ) ) {
// WooCommerce company city
update_user_meta( $customer_id, 'city', sanitize_text_field( $_POST['billing_city'] ) );
// WooCommerce billing company city
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_mol'] ) ) {
// WooCommerce company mol
update_user_meta( $customer_id, 'mol', sanitize_text_field( $_POST['billing_mol'] ) );
// WooCommerce billing company mol
update_user_meta( $customer_id, 'billing_mol', sanitize_text_field( $_POST['billing_mol'] ) );
}
if ( isset( $_POST['billing_adres'] ) ) {
// WooCommerce company address
update_user_meta( $customer_id, 'adres', sanitize_text_field( $_POST['billing_adres'] ) );
// WooCommerce billing company address
update_user_meta( $customer_id, 'billing_adres', sanitize_text_field( $_POST['billing_adres'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
add_action( 'woocommerce_checkout_process', 'ws_billing_fields_save', 10, 1 );
function ws_billing_fields_save( $user_id ){
if ( isset( $_POST['billing_bulstat'] ) ) {
update_user_meta($user_id, 'billing_bulstat', $_POST['billing_bulstat']);
}
if ( isset( $_POST['billing_mol'] ) ) {
update_user_meta($user_id, 'billing_mol', $_POST['billing_mol']);
}
if ( isset( $_POST['billing_adres'] ) ) {
update_user_meta($user_id, 'billing_adres', $_POST['billing_adres']);
}
}
/**
* Dopylnitelni poleta v User Profil na Dashboard v Wordpress
*/
function eri_add_custom_user_profile_fields( $user ) {
?>
<!-- Field Title -->
<h3><?php _e('Company Information', 'eribootstrap'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="billing_bulstat">
<?php _e('ЕИК', 'eribootstrap'); ?>
</label>
</th>
<td>
<input type="text" name="billing_bulstat" id="billing_bulstat" value="<?php echo
esc_attr( get_the_author_meta( 'billing_bulstat', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Въведи ЕИК.', 'eribootstrap'); ?></span>
</td>
</tr><!-- field ends here -->
<tr>
<th>
<label for="billing_mol"><?php _e('МОЛ', 'eribootstrap'); ?>
</label></th>
<td>
<input type="text" name="billing_mol" id="billing_mol" value="<?php echo esc_attr(
get_the_author_meta( 'billing_mol', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Въведете МОЛ на фирмата', 'eribootstrap'); ?
></span>
</td>
</tr><!-- field ends here -->
<tr>
<th>
<label for="billing_adres"><?php _e('Адрес за фактуриране', 'eribootstrap'); ?>
</label></th>
<td>
<textarea name="billing_adres" id="billing_adres" class="regular-text"><?php echo esc_attr( get_the_author_meta(
'billing_adres', $user->ID ) ); ?>
</textarea><br />
<span class="description"><?php _e('Въведете адрес за фактуриране', 'eribootstrap'); ?></span>
</td>
</tr><!-- field ends here -->
</table>
<?php }
function eri_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
// Update and Save Field
update_usermeta( $user_id, 'billing_bulstat', $_POST['billing_bulstat'] );
update_usermeta( $user_id, 'billing_mol', $_POST['billing_mol'] );
update_usermeta( $user_id, 'billing_adres', $_POST['billing_adres'] );
}
add_action( 'show_user_profile', 'eri_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'eri_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'eri_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'eri_save_custom_user_profile_fields' );
The second plugin adds three new custom field on checkout page. They are displayed, but are not automaticaly populated. I use $checkout->get_value( 'my_field_name' ) ); function, but is doesn't work.
I want they to be automatic populated with the presaved values. The fields are the same: 'billing_bulstat' ; 'billing_mol' and 'billing_adres'. The code is:
<?php
/*
Plugin Name: Woocommerce Checkout fileds
Plugin URI: https://www.luga.bg/traders
Description: Customized Woocommerce checkout fields
Version: 1
Author: Nikolay Grudev
Author URI: http://www.luga.bg/
*/
/**
* set customized Woocommerce checkout fields
*/
add_filter( 'woocommerce_checkout_fields' , 'customize_fields' );
function customize_fields( $fields ) {
// make fields required:
$fields['billing']['billing_company']['required'] = true;
return $fields;
}
// Remove some checkout billing fields
function kia_filter_billing_fields($fields){
unset( $fields["billing_country"] );
unset( $fields["billing_address_1"] );
unset( $fields["billing_address_2"] );
unset( $fields["billing_state"] );
unset( $fields["billing_postcode"] );
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'kia_filter_billing_fields' );
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('Extra Information') . '</h2>';
woocommerce_form_field(
'billing_bulstat', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('EIK'),
'placeholder' => __('Enter something'),
'required' => true,
),$checkout->get_value( 'billing_bulstat' ) );
woocommerce_form_field(
'billing_mol', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('MOL'),
'placeholder' => __('Enter something'),
'required' => true,
), $checkout->get_value( 'billing_mol' ) );
woocommerce_form_field(
'billing_adres', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Adres za fakturirane'),
'placeholder' => __('Enter something'),
'required' => true,
), $checkout->get_value( 'billing_adres' ) );
echo '</div>';
}
/**
* Validate the custom field.
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_bulstat'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
if ( ! $_POST['billing_mol'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
if ( ! $_POST['billing_adres'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
/**
* Save the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['billing_bulstat'] ) ) {
update_post_meta( $order_id, 'billing_bulstat', sanitize_text_field( $_POST['billing_bulstat'] ) );
}
if ( ! empty( $_POST['billing_mol'] ) ) {
update_post_meta( $order_id, 'billing_mol', sanitize_text_field( $_POST['billing_mol'] ) );
}
if ( ! empty( $_POST['billing_adres'] ) ) {
update_post_meta( $order_id, 'billing_adres', sanitize_text_field( $_POST['billing_adres'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address',
'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Extra Information').':</strong> ' . get_post_meta( $order->id, 'billing_bulstat', true )
. '</p>';
echo '<p><strong>'.__('Extra Information').':</strong> ' . get_post_meta( $order->id, 'billing_mol', true ) .
'</p>';
echo '<p><strong>'.__('Extra Information').':</strong> ' . get_post_meta( $order->id, 'billing_adres', true ) .
'</p>';
}
// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){ ?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'Bulstat: ' ); ?></th>
<td><?php echo get_post_meta( $order_id, 'billing_bulstat', true ); ?></td>
</tr>
<tr>
<th><?php _e( 'MOL: ' ); ?></th>
<td><?php echo get_post_meta( $order_id, 'billing_mol', true ); ?></td>
</tr>
<tr>
<th><?php _e( 'Adres za fakturirane: ' ); ?></th>
<td><?php echo get_post_meta( $order_id, 'billing_adres', true ); ?></td>
</tr>
</tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Danni za firmata', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Firma: ' ) . '</strong>' . get_post_meta( $order->id, '_billing_company', true
) . '</p>';
echo '<p><strong>' . __( 'Bulstat: ' ) . '</strong>' . get_post_meta( $order->id, 'billing_bulstat',
true ) . '</p>';
echo '<p><strong>' . __( 'MOL: ' ) . '</strong>' . get_post_meta( $order->id, 'billing_mol', true ) .
'</p>';
echo '<p><strong>' . __( 'Adres za fakturirane: ' ) . '</strong>' . get_post_meta( $order-
>id, 'billing_adres', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
// WooCommerce 2.3+
function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['billing_bulstat'] = array(
'label' => __( 'Bulstat' ),
'value' => get_post_meta( $order->id, 'billing_bulstat', true ),
);
$fields['billing_mol'] = array(
'label' => __( 'MOL' ),
'value' => get_post_meta( $order->id, 'billing_mol', true ),
);
$fields['billing_adres'] = array(
'label' => __( 'Adrez za fakturirane' ),
'value' => get_post_meta( $order->id, 'billing_adres', true ),
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields', 10, 3 );
?>
Where I am wrong? Why I my fields are not populated atomaticaly with their values?
Thanks for help!
the site is www.luga.bg/traders