Adding Custom Shipping And Billing Fields

Adding fields is done in a similar way to overriding fields. For example, let’s add a new field to shipping fields – shipping_phone:

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function – $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['shipping']['shipping_phone'] = array(
        'label'     => __('Phone', 'woocommerce'),
    'placeholder'   => _x('Phone', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

/**
 * Display field value on the order edit page
 */
 
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Phone From Checkout Form').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
gist: https://gist.github.com/woogists/7241afdfa6f21d561ce85f7247a0f282#file-wc-override-checkout-fields-php

Adding a Custom Special Field

To add a custom field is similar. Let’s add a new field to checkout, after the order notes, by hooking into the following:

/**
 * 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>' . __('My Field') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'   => __('Enter something'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

}
gist: https://gist.github.com/woogists/981c3b405b4288c562a64f59db9c5e13#file-wc-custom-checkout-field-php

You can also add those field to order summary and email notification. To see how this is done go to src posts and continue reading.

Adding Custom Fields To Emails

To add a custom field value to WooCommerce emails — a completed order email, for example — use the following snippet: see here

Read also: Add custom field to Woocommerce billing form

src:
https://jeroensormani.com/ultimate-guide-to-woocommerce-checkout-fields/

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-6

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-7

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#
0
Would love your thoughts, please comment.x
()
x