I EDIT THE QUESTION
What should I do to reorder the new Checkout fields created to my needs?
What should I do so that these fields are only shown to certain users according to their role?
It is important to show this new field only to certain user roles
I have not formulated the question correctly, so I correct it.
This new NIF/CIF field should only be shown to a certain user role, in this case the role
is: "wholesaler_customer".
Only if the user is registered and has this role ("wholesaler_customer") should the field be shown at Checkout
.
For the rest of the user roles
, this field will not be visible in the Checkout
Thanks to the collaboration of another user of this site @MartinMirchev , who provided me with documentation to review, I have managed to isolate the problem a bit.
The new field is not reordered, but duplicated, and is displayed at the bottom, after the email address field, and the "Ship to a different address?" checkbox.
The new field that I have created is shown in the position that I indicate with the Code Priority, but it only shows the entry, the placeholder or the label that I have assigned to it is not shown (NIF/CIF: ) To reorder I use the second code snippet below.
What am I doing wrong in these functions?
I have a new field in the woocommerce checkout for the NIF of the companies. The following function creates the field and displays it on the form.
/* show the new field on the checkout page */
add_action( 'woocommerce_before_order_notes', 'bbloomer_add_custom_checkout_field' );
function bbloomer_add_custom_checkout_field( $checkout ) {
$current_user = wp_get_current_user();
$saved_license_no = $current_user->license_no;
woocommerce_form_field( 'license_no', array(
'type' => 'text',
'class' => array( 'form-row-wide' ),
'label' => 'NIF/CIF',
'placeholder' => 'B12345678',
'required' => true,
'default' => $saved_license_no,
), $checkout->get_value( 'license_no' ) );
}
/* make sure the new field is not empty */
add_action( 'woocommerce_checkout_process', 'bbloomer_validate_new_checkout_field' );
function bbloomer_validate_new_checkout_field() {
if ( ! $_POST['license_no'] ) {
wc_add_notice( 'Por favor ingrese su número de NIF', 'error' );
}
}
/* save the new field and show it in email, checkout, etc ...*/
add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field' );
function bbloomer_save_new_checkout_field( $order_id ) {
if ( $_POST['license_no'] ) update_post_meta( $order_id, '_license_no', esc_attr( $_POST['license_no'] ) );
}
add_action( 'woocommerce_thankyou', 'bbloomer_show_new_checkout_field_thankyou' );
function bbloomer_show_new_checkout_field_thankyou( $order_id ) {
if ( get_post_meta( $order_id, '_license_no', true ) ) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta( $order_id, '_license_no', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order' );
function bbloomer_show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_license_no', true ) ) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta( $order_id, '_license_no', true ) . '</p>';
}
add_action( 'woocommerce_email_after_order_table', 'bbloomer_show_new_checkout_field_emails', 20, 4 );
function bbloomer_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_license_no', true ) ) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta( $order->get_id(), '_license_no', true ) . '</p>';
}
Below I show the function to reorder the fields of the form. By default, the new field is displayed at the bottom of the form. Of course, these functions work for other users, and are available to anyone, as they show in the "Business Bloomer" tutorials and articles
Here you will find more functions where I found inspiration to reorder the payment form fields With this function, I am changing the priority and it should place the new field (NIF/CIF) , below the "Company Name (optional)" field.
add_filter( 'woocommerce_default_address_fields', 'bbloomer_reorder_checkout_fields' );
function bbloomer_reorder_checkout_fields( $fields ) {
// default priorities:
// 'first_name' - 10
// 'last_name' - 20
// 'company' - 30
// 'country' - 40
// 'address_1' - 50
// 'address_2' - 60
// 'city' - 70
// 'state' - 80
// 'postcode' - 90
// e.g. move 'company' above 'first_name':
// just assign priority less than 10
$fields['license_no']['priority'] = 35;
return $fields;
}
The position does not change, it is duplicated, but at the top where I need the new field to be, only the input drawer is shown, and the NIF/CIF label is not shown, and the placeholder is not shown either.
What do I need to correct in the function so that everything is displayed perfectly?
What should I do so that this field that I have created is shown only to certain users?
What should I correct in the function so that everything is displayed perfectly?
Remove add_action( 'woocommerce_before_order_notes', 'bbloomer_add_custom_checkout_field' );
and add_filter( 'woocommerce_default_address_fields', 'bbloomer_reorder_checkout_fields' );
from your code
You can use this bellow code to do all things that you need (set position of input field & show input field only for certain user role)
<?php
add_filter('woocommerce_checkout_fields', 'add_custom_woocommerce_checkout_field');
/**
* Add a custom field for NIF/CIF to the WooCommerce checkout form
*
* @param array $fields An array of checkout fields
* @return array An updated array of checkout fields with the NIF/CIF field added
*/
function add_custom_woocommerce_checkout_field( $fields ) {
// Only add this field when user is administrator
if ( has_user_role( get_current_user_id(), 'administrator' ) ) {
$current_user = wp_get_current_user();
$saved_license_no = $current_user->license_no;
// Add the NIF/CIF field to the billing section of the checkout form
$fields['billing']['license_no'] = array(
'label' => __( 'NIF/CIF', 'woocommerce' ),
'placeholder' => __( 'B12345678', 'woocommerce' ),
'required' => true,
'clear' => false,
'type' => 'text',
'default' => $saved_license_no,
'class' => array( 'form-row-wide' ),
'priority' => 25, // Change here to set position
);
}
// Return the updated array of checkout fields
return $fields;
}
/**
* Check user role
*
* @param int $user_id
* @param string $role
* @return boolean
*/
function has_user_role( $user_id, $role ) {
$user = get_userdata( $user_id );
if ( ! empty( $user ) && in_array( $role, (array) $user->roles ) ) {
return true;
}
return false;
}
CHECK THIS IMAGES :
Field is displayed in these :
View order section in my account page
and any other email ( New order, Cancelled order, Failed order, Order on-hold, Processing order, Completed order, Customer invoice / Order details ), in every those field is there like this two email image i've added here
NOTE : use my code with this below code that you already used in your question. I've added one more action hook in it
<?php
/* make sure the new field is not empty */
add_action( 'woocommerce_checkout_process', 'bbloomer_validate_new_checkout_field' );
function bbloomer_validate_new_checkout_field() {
if ( ! isset($_POST['license_no']) && has_user_role( get_current_user_id(), 'administrator' ) ) {
wc_add_notice( 'Por favor ingrese su número de NIF', 'error' );
}
}
/* save the new field and show it in email, checkout, etc ...*/
add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field' );
function bbloomer_save_new_checkout_field( $order_id ) {
if ( isset($_POST['license_no']) ) update_post_meta( $order_id, '_license_no', esc_attr( $_POST['license_no'] ) );
}
add_action( 'woocommerce_thankyou', 'bbloomer_show_new_checkout_field_thankyou' );
function bbloomer_show_new_checkout_field_thankyou( $order_id ) {
if ( get_post_meta( $order_id, '_license_no', true ) ) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta( $order_id, '_license_no', true ) . '</p>';
}
add_filter( 'woocommerce_order_details_after_order_table' , 'bbloomer_show_new_checkout_field_order', 20, 1 );
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order' );
function bbloomer_show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_license_no', true ) ) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta( $order_id, '_license_no', true ) . '</p>';
}
add_action( 'woocommerce_email_after_order_table', 'bbloomer_show_new_checkout_field_emails', 20, 4 );
function bbloomer_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_license_no', true ) ) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta( $order->get_id(), '_license_no', true ) . '</p>';
}