Add custom fields under Billing Fields and save in Order Meta

Add custom fields under Billing Fields and save in Order Meta
function comcast_woocommerce_billing_fields($fields)
{
    $fields['gx_name'] = array(
        'label'       => __('Billing Contact for GXS', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required'    => true, // if field is required or not
        'clear'       => false, // add clear or not
        'type'        => 'text', // add field type
        'class'       => array('gx_name')    // add class name
    );
    $fields['gx_email'] = array(
        'label'       => __('Billing Contact Email Address', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required'    => true, // if field is required or not
        'clear'       => false, // add clear or not
        'type'        => 'text', // add field type
        'class'       => array('gx_email')    // add class name
    );
    return $fields;
}
add_filter('woocommerce_billing_fields', 'comcast_woocommerce_billing_fields');
function comcast_save_extra_checkout_fields( $order_id, $posted ){
    if( isset( $posted['gx_name'] ) ) {
        update_post_meta( $order_id, '_gx_name', sanitize_text_field( $posted['gx_name'] ) );
    }
    if( isset( $posted['gx_email'] ) ) {
        update_post_meta( $order_id, '_gx_email', sanitize_text_field( $posted['gx_email'] ) );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'comcast_save_extra_checkout_fields', 10, 2 );

screen38

screen39

Leave a Reply

Your email address will not be published. Required fields are marked *