I’ve added two custom fields to my checkout form - first to collect VAT ID, second to company name (default company name field being turned off).

here is my code:

add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field2' );
function my_custom_checkout_field2( $checkout ) {
    echo '<div id="my_custom_checkout_field2">';
    woocommerce_form_field( 'my_field_name2', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Company'),
        'placeholder'   => __('Enter company name'),
        ), $checkout->get_value( 'my_field_name2' ));
    echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field2_update_order_meta' );
function my_custom_checkout_field2_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['my_field_name2'] ) ) {
        update_post_meta( $order_id, 'Company', sanitize_text_field( $_POST['my_field_name2'] ) );
    }
}
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field">';
    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('VAT ID'),
        'placeholder'   => __('Enter VAT ID'),
        ), $checkout->get_value( 'my_field_name' ));
    echo '</div>';
}
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['my_field_name'] ) ) {
        update_post_meta( $order_id, 'VAT ID', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}
// ORDER update
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>'.__('VAT ID').':</strong> ' . get_post_meta( $order->id, 'VAT ID', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field2_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field2_display_admin_order_meta($order){
    echo '<p><strong>'.__('Company').':</strong> ' . get_post_meta( $order->id, 'Company', true ) . '</p>';
}
//USER update
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field2_update_user_meta');
function my_custom_checkout_field2_update_user_meta( $user_id ) {
    if ($user_id && $_POST['my_field_name2']) update_user_meta( $user_id, 'my_field_name2', esc_attr($_POST['my_field_name2']) );
}

So far i manage to display collected data at order details. THe goal is to display it at order summary page (after clicking ORder Button) and at User Page (user billing details). And how to add those two fields to user page?

Read more here: Woocommerce custom fcheckout fileds - -how to display at summary and user page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , , , ,

Wordpress related questions and answers: