---
title: "How do you make the telephone compulsory and show it on the invoice next to the CIF/NIF in WooCommerce 2.1?"
description: "After the request from one of our clients, we had to give some thought to the code we put to you at the time in the post How do you add a NIF or CIF field to WooCommerce 2.1?, in"
url: https://artprojectgroup.com/how-to-make-the-telephone-compulsory-and-show-it-on-the-invoice-next-to-the-cif-nif-in-woocommerce-2-1
date: 2014-07-29
modified: 2023-02-11
author: "Art Project Group"
image: https://artprojectgroup.com/wp-content/uploads/2023/04/woocommerce-blog.webp
categories: ["Tutorials"]
tags: ["functions.php", "WooCommerce", "WordPress"]
type: post
lang: en-US
---

# How do you make the telephone compulsory and show it on the invoice next to the CIF/NIF in WooCommerce 2.1?

After the request from one of our clients, we had to give some thought to the code we put to you at the time in the post [How do you add a NIF or CIF field to WooCommerce 2.1?](/how-to-add-a-nif-or-cif-field-with-validation-to-woocommerce-2-4), in order to get the customer’s telephone to be displayed, as well as the NIF/CIF in all the WooCommerce 2.1 address fields, and for it also to be compulsory. The reason? He needed it so that the transport company could get in touch with the customer.

Well then, so that you can see that it is really quite simple, first we are going to put the complete PHP code that needs to be added to the *functions.php* file of your child theme, and afterwards we will analyse the lines we have modified from the code published in the post [How do you add a NIF or CIF field to WooCommerce 2.1?](/how-to-add-a-nif-or-cif-field-with-validation-to-woocommerce-2-4):

Copy to clipboardSyntax Highlighter//We fix the default address
function address_fields( $fields ) {
$fields[ 'nif' ] = array(
'label' => __( 'CIF/NIF', 'woocommerce' ),
'placeholder' => _x( 'Introduzca elCIF/NIF', 'placeholder', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-last' ),
'clear' => true,
);

$fields[ 'company' ]\[ 'class' ]\[ 0 ] = 'form-row-first';
$fields[ 'city' ]\[ 'class' ]\[ 0 ] = 'form-row-first';
$fields[ 'state' ]\[ 'class' ]\[ 0 ] = 'form-row-last update_totals_on_change';
$fields[ 'postcode' ]\[ 'class' ]\[ 0 ] .= ' update_totals_on_change';

//We reorder the fields
$new_fields[ 'country' ] = $fields[ 'country' ];
$new_fields[ 'first_name' ] = $fields[ 'first_name' ];
$new_fields[ 'last_name' ] = $fields[ 'last_name' ];
$new_fields[ 'company' ] = $fields[ 'company' ];
$new_fields[ 'nif' ] = $fields[ 'nif' ];
$new_fields[ 'address_1' ] = $fields[ 'address_1' ];
$new_fields[ 'address_2' ] = $fields[ 'address_2' ];
$new_fields[ 'postcode' ] = $fields[ 'postcode' ];
$new_fields[ 'city' ] = $fields[ 'city' ];
$new_fields[ 'state' ] = $fields[ 'state' ];
if ( isset( $fields[ 'email' ] ) ) {
$new_fields[ 'email' ] = $fields[ 'email' ];
}
if ( isset( $fields[ 'phone' ] ) ) {
$new_fields[ 'phone' ]\[ 'required' ] = true;
$new_fields[ 'phone' ] = $fields[ 'phone' ];
}

return $new_fields;
}
add_filter( 'woocommerce_default_address_fields', 'address_fields' );

//New function to make the code compatible with WooCommerce 2.1
function get_custom_field( $field, $order ) {
$value = get_post_meta( $order, $field, false );
if ( isset( $value[ 0 ] ) ) {
return $value[ 0 ];
}
return NULL;
}

//We add the NIF and the telephone to the billing and shipping address
function add_nif_field_to_billing_address( $fields, $order ) {
$fields[ 'nif' ] = get_custom_field( '_billing_nif', $order->id );
$fields[ 'phone' ] = get_custom_field( '_billing_phone', $order->id );

return $fields;
}
add_filter( 'woocommerce_order_formatted_billing_address', 'add_nif_field_to_billing_address', 1, 2 );

function add_nif_field_to_shipping_address( $fields, $order ) {
$fields[ 'nif' ] = get_custom_field( '_shipping_nif', $order->id );
$fields[ 'phone' ] = get_custom_field( '_shipping_phone', $order->id );

return $fields;
}
add_filter( 'woocommerce_order_formatted_shipping_address', 'add_nif_field_to_shipping_address', 1, 2 );

function billing_address_format( $fields, $argumentos ) {
$fields[ '{nif}' ] = $argumentos[ 'nif' ];
$fields[ '{nif_upper}' ] = strtoupper( $argumentos[ 'nif' ] );
$fields[ '{phone}' ] = $argumentos[ 'phone' ];
$fields[ '{phone_upper}' ] = strtoupper( $argumentos[ 'phone' ] );

return $fields;
}
add_filter( 'woocommerce_formatted_address_replacements', 'billing_address_format', 1, 2 );

//We reorder the fields of the default address
function localisation_address_format( $fields ) {
$fields[ 'default' ] = "{name}n{company}n{nif}n{address_1}n{address_2}n{city}n{state}n{postcode}n{country}n{phone}";
$fields[ 'ES' ] = "{name}n{company}n{nif}n{address_1}n{address_2}n{postcode} {city}n{state}n{country}n{phone}";

return $fields;
}
add_filter( 'woocommerce_localisation_address_formats', 'localisation_address_format' );

//We fix the shipping form
function shipping_form( $fields ) {
$fields[ 'shipping_email' ] = array(
'label' => __( 'EmailAddress', 'woocommerce' ),
'required' => false,
'class' => array( 'form-row-first' ),
'validate' => array( 'email' ),
);
$fields[ 'shipping_phone' ] = array(
'label' => __( 'Phone', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-last' ),
'clear' => true,
);
$fields[ 'shipping_postcode' ] = array(
'label' => __( 'Postcode /Zip', 'woocommerce' ),
'placeholder' => __( 'Postcode /Zip', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide', 'address-field' ),
'clear' => true,
'custom_attributes' => array(
'autocomplete' => 'no'
)
);

return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'shipping_form' );

//We fix the billing form
function billing_form( $fields ) {
$fields[ 'billing_postcode' ] = array(
'label' => __( 'Postcode /Zip', 'woocommerce' ),
'placeholder' => __( 'Postcode /Zip', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide', 'address-field' ),
'clear' => true,
'custom_attributes' => array(
'autocomplete' => 'no'
)
);

return $fields;
}
add_filter( 'woocommerce_billing_fields', 'billing_form' );

//Adds the CIF/NIF field to users
function add_fields_to_user_admin( $fields ) {
$fields[ 'billing' ]\[ 'fields' ]\[ 'billing_nif' ] = array(
'label' => __( 'CIF/NIF', 'woocommerce' ),
'description' => ''
);

$fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_nif' ] = array(
'label' => __( 'CIF/NIF', 'woocommerce' ),
'description' => ''
);
$fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_email' ] = array(
'label' => __( 'Email', 'woocommerce' ),
'description' => ''
);
$fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_phone' ] = array(
'label' => __( 'Telephone', 'woocommerce' ),
'description' => ''
);

//We reorder the fields
$new_fields[ 'billing' ]\[ 'title' ] = $fields[ 'billing' ]\[ 'title' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_first_name' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_first_name' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_last_name' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_last_name' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_company' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_company' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_nif' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_nif' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_address_1' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_address_1' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_address_2' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_address_2' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_postcode' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_postcode' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_city' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_city' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_state' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_state' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_country' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_country' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_phone' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_phone' ];
$new_fields[ 'billing' ]\[ 'fields' ]\[ 'billing_email' ] = $fields[ 'billing' ]\[ 'fields' ]\[ 'billing_email' ];

$new_fields[ 'shipping' ]\[ 'title' ] = $fields[ 'shipping' ]\[ 'title' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_first_name' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_first_name' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_last_name' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_last_name' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_company' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_company' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_nif' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_nif' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_address_1' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_address_1' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_address_2' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_address_2' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_postcode' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_postcode' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_city' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_city' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_state' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_state' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_country' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_country' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_phone' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_phone' ];
$new_fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_email' ] = $fields[ 'shipping' ]\[ 'fields' ]\[ 'shipping_email' ];

$new_fields = apply_filters( 'wcbcf_customer_meta_fields', $new_fields );

return $new_fields;
}
add_filter( 'woocommerce_customer_meta_fields', 'add_fields_to_user_admin' );

//We add the NIF to the billing and shipping address
function add_nif_field_to_user_billing_address( $fields, $user ) {
$fields[ 'nif' ] = get_user_meta( $user, 'billing_nif', true );
$fields[ 'phone' ] = get_user_meta( $user, 'billing_phone', true );
return $fields;
}
add_filter( 'woocommerce_user_column_billing_address', 'add_nif_field_to_user_billing_address', 1, 2 );

function add_nif_field_to_user_shipping_address( $fields, $user ) {
$fields[ 'nif' ] = get_user_meta( $user, 'shipping_nif', true );
$fields[ 'phone' ] = get_user_meta( $user, 'shipping_phone', true );
return $fields;
}
add_filter( 'woocommerce_user_column_shipping_address', 'add_nif_field_to_user_shipping_address', 1, 2 );

//Adds the NIF field to Edit my address
function add_nif_field_to_edit_address( $fields, $user, $name ) {
$fields[ 'nif' ] = get_user_meta( $user, $name . '_nif', true );
$fields[ 'phone' ] = get_user_meta( $user, $name . '_phone', true );

//Sorts the fields
$new_fields[ 'first_name' ] = $fields[ 'first_name' ];
$new_fields[ 'last_name' ] = $fields[ 'last_name' ];
$new_fields[ 'company' ] = $fields[ 'company' ];
$new_fields[ 'nif' ] = $fields[ 'nif' ];
$new_fields[ 'address_1' ] = $fields[ 'address_1' ];
$new_fields[ 'address_2' ] = $fields[ 'address_2' ];
$new_fields[ 'postcode' ] = $fields[ 'postcode' ];
$new_fields[ 'city' ] = $fields[ 'city' ];
$new_fields[ 'state' ] = $fields[ 'state' ];
$new_fields[ 'country' ] = $fields[ 'country' ];
$new_fields[ 'phone' ] = $fields[ 'phone' ];

return $new_fields;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address', 'add_nif_field_to_edit_address', 10, 3 );

//Adds the NIF field to Order details
function add_nif_field_to_order_edit_address( $fields ) {
$fields[ 'nif' ] = array(
'label' => __( 'CIF/NIF', 'woocommerce' ),
'show' => false
);
$fields[ 'phone' ] = array(
'label' => __( 'Telephone', 'woocommerce' ),
'show' => false
);

//Sorts the fields
$new_fields[ 'first_name' ] = $fields[ 'first_name' ];
$new_fields[ 'last_name' ] = $fields[ 'last_name' ];
$new_fields[ 'company' ] = $fields[ 'company' ];
$new_fields[ 'nif' ] = $fields[ 'nif' ];
$new_fields[ 'address_1' ] = $fields[ 'address_1' ];
$new_fields[ 'address_2' ] = $fields[ 'address_2' ];
$new_fields[ 'postcode' ] = $fields[ 'postcode' ];
$new_fields[ 'city' ] = $fields[ 'city' ];
$new_fields[ 'state' ] = $fields[ 'state' ];
$new_fields[ 'country' ] = $fields[ 'country' ];
$new_fields[ 'phone' ] = $fields[ 'phone' ];

return $new_fields;
}
add_filter( 'woocommerce_admin_billing_fields', 'add_nif_field_to_order_edit_address' );
add_filter( 'woocommerce_admin_shipping_fields', 'add_nif_field_to_order_edit_address' );

function load_order_edit_address_stylesheet() {
echo '

';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'load_order_edit_address_stylesheet' );
In it we have made the following modifications:

1. The *campos_de_direccion* function, where we have told it that the *telephone* field is compulsory.
2. The *anade_campo_nif_direccion_facturacion* function, where we have added the *telephone* field to the billing address.
3. The *anade_campo_nif_direccion_envio* function, where we have added the *telephone* field to the shipping address.
4. The *formato_direccion_de_facturacion* function, where we have added the *telephone* field to the replacement fields in the local address format.
5. The *formato_direccion_localizacion* function, where we have added the *telephone* field to the local, Spanish, address format.
6. The *anade_campo_nif_usuario_direccion_facturacion* function, where we have added the *telephone* field to the address form.
7. The *anade_campo_nif_usuario_direccion_envio* function, where we have added the *telephone* field to the shipping form.
8. The *anade_campo_nif_editar_direccion* function, where we have added the *telephone* field to the address forms of the *My account* page.
9. The *anade_campo_nif_editar_direccion_pedido* function, where we have added the *telephone* field to the address forms of *Orders* in the WooCommerce *administration panel*.
10. The *carga_hoja_de_estilo_editar_direccion_pedido* function, where we edit the CSS code of the *telephone* field of the previous forms.

With these modifications we will have covered 100% of the needs that WooCommerce poses to us when it comes to making a field compulsory, in this case the *telephone*, and to displaying it in the order and on the invoice next to another field that is not “native”, such as the NIF/CIF field.

We hope that, as always, you like this post and find it useful, and we look forward to your comments.
