Populating custom fields on checkout page - php

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

Related

PHP/Dokan/WooCommerce. Change input type="text" to input="checkbox"

We have a WordPress site using Dokan.
I'd like to add a checkbox where the user can check whether the product should be a donation.
I got this snippet that I'm trying to modify. My PHP is a bit too basic. Please help.
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group">
<input type="text" class="dokan-form-control" name="new_field" placeholder="<?php esc_attr_e( 'Product Code', 'dokan-lite' ); ?>">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['new_field'] ) ) {
update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$new_field = get_post_meta( $post_id, 'new_field', true );
?>
<div class="dokan-form-group">
<input type="hidden" name="new_field" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Product Code', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'new_field', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $new_field ) ); ?>
<div class="dokan-product-title-alert dokan-hide">
<?php esc_html_e( 'Please enter product code!', 'dokan-lite' ); ?>
</div>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
$new_field = get_post_meta( $product->get_id(), 'new_field', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Product Code:', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}
This is how far I got:
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group">
<input type="checkbox" class="dokan-form-control" name="new_field" placeholder="<?php esc_attr_e( 'Product Code', 'dokan-lite' ); ?>">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['new_field'] ) ) {
update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$new_field = get_post_meta( $post_id, 'new_field', true );
?>
<div class="dokan-form-group">
<input type="hidden" name="new_field" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Product Code', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'new_field', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $new_field ) ); ?>
<div class="dokan-product-title-alert dokan-hide">
<?php esc_html_e( 'Please enter product code!', 'dokan-lite' ); ?>
</div>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
$new_field = get_post_meta( $product->get_id(), 'new_field', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Donation Product', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}
The rest I'm struggling with. Many thanks for any help on this.

Wordpress child theme functions.php not working

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.

Populating Woocommerce Custom Fields

I'm having issues populating information gained through custom fields on a Woocommerce sign up page, when filling out the form the only things that seem to get pulled over are: First Name, Last Name, City, Postcode, Phone & Email Address.
When a user signs up for our site, they cannot immediately login as their account needs to be approved by an admin, the information pulling through will be used to contact the person prior to their account being activated.
I have tried dumping the $_POSTarray by using var_dump($_POST) but after making the change, none of the fields were working correctly.
Please see below what I'm currently using to add custom fields & to populate fields on the account page:
This is what I am using to add the custom fields to my woocommerce registration form:
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-wide">
<label for="reg_billing_company"><?php _e( 'Company', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php 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 esc_attr_e( $_POST['billing_phone'] ); ?>" /></p>
<p class="form-row form-row-wide">
<label for="reg_billing_adress_1"><?php _e( 'Adress Line 1', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_adress_1" id="reg_billing_adress_1" value="<?php esc_attr_e( $_POST['billing_address_1'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_adress_2"><?php _e( 'Adress Line 2', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_adress_2" id="reg_billing_adress_2" value="<?php esc_attr_e( $_POST['billing_address_2'] ); ?>" />
</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( 'Postcode', '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
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
This is what I'm using to populate the backend 'user' area once a user signs up:
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']));
}
//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_adress_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 Adress 2 Field
if (isset($_POST['billing_adress_2'])) {
update_user_meta($customer_id, 'address_2', sanitize_text_field($_POST['billing_address_2']));
update_user_meta($customer_id, 'billing_address_2', sanitize_text_field($_POST['billing_address_2']));
}
//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']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
I'm not the most experienced in PHP, so any help would be massively appreciated.
There is some mistakes in your first hooked function. Also "Company" and "Address 2" fields shouldn't be required.
In your 2nd hooked function, "Company" is missing, so it's not saved anyway.
To finish You need an additional hooked function for fields required validation.
So your code should be:
// 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 Line 1', '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_address_2"><?php _e( 'Adress Line 2', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if ( ! empty( $_POST['billing_address_2'] ) ) esc_attr_e( $_POST['billing_address_2'] ); ?>" />
</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 if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
</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>
<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 City Field
if ( isset($_POST['billing_city']) && empty( $_POST['billing_city'] ) )
$validation_errors->add( 'billing_city_error', $error . __( 'City 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 ) );
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 Adress 2 Field
if ( isset( $_POST['billing_address_2'] ) ) {
update_user_meta( $customer_id, 'address_2', sanitize_text_field( $_POST['billing_address_2'] ) );
update_user_meta( $customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] ) );
}
// 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'] ) );
}
}
This code goes on function.php file of your active child theme (or theme). Tested and works.

Adding an additional custom field in Woocommerce Edit Account page

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.

Assign user role autometically based on country while user registration in woocommerse

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');

Categories