How to add/edit/delete custom checkout fields and send them into email in your woocommerce shop?

/**
 * Ordering custom checkout fields
 */

// Billing Fields.
add_filter( 'woocommerce_billing_fields' , 'woocommerce_billing_fields_custom' );

function woocommerce_billing_fields_custom( $fields ) {

    //Set existing fields

    $fields['billing_first_name']['label'] = 'Name of Corresponding Author';
    $fields['billing_phone']['maxlength'] = 10;
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['class'] = array( 'form-row-first' );
    $fields['billing_state']['class'] = array( 'form-row-last' );
    $fields['billing_postcode']['maxlength'] = 6;
    $fields['billing_postcode']['class'] = array( 'form-row-first' );

    //Adding new fields

    $fields['billing_article_title'] = array(
        'label' => __('Article Title', '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('form-row-first')    // add class name
    );

    $fields['billing_article_id'] = array(
        'label' => __('Article Submission Id', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => true, // if field is required or not
        'clear' => true, // add clear or not
        'type' => 'number', // add field type
        'class' => array('form-row-last')    // add class name
    );

    $fields['billing_alternate_phone'] = array(
        'label' => __('Alternate Phone Number', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => true, // add clear or not
        'type' => 'number', // add field type
        'class' => array('form-row-last')    // add class name
    );

    //Set Priorities of Your Billing form fields

    $fields['billing_first_name']['priority'] = 10;
    $fields['billing_email']['priority'] = 15;
    $fields['billing_article_title']['priority'] = 20;
    $fields['billing_article_id']['priority'] = 25;
    $fields['billing_phone']['priority'] = 30;
    $fields['billing_alternate_phone']['priority'] = 35;
    $fields['billing_address_1']['priority'] = 40;
    $fields['billing_address_2']['priority'] = 45;
    $fields['billing_country']['priority'] = 60;
    $fields['billing_state']['priority'] = 61;
    $fields['billing_city']['priority'] = 62;
    $fields['billing_postcode']['priority'] = 63;

    return $fields;
}

add_filter("woocommerce_checkout_fields", "order_fields");

  //Now order all fields in the billing form woocommerce checkout

function order_fields($fields) {

    $order = array(
        "billing_first_name",
        "billing_email",
        "billing_article_title",
        "billing_article_id",
        "billing_phone",
        "billing_alternate_phone",
        "billing_address_1",
        "billing_address_2",
        "billing_country",
        "billing_state",
        "billing_city",
        "billing_postcode",
    );
    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;
    return $fields;

}


/**
 * Validate fields if needed
 * In this case we normally validating phone only
 * validating billing phone number as 10 digit only
 */

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error. This one is only requite for companies
    if ( ! (preg_match('/^[0-9]{10}$/D', $_POST['billing_phone'] ))){
        wc_add_notice( "The Phone number should contain only 10 digits"  ,'error' );
    }

    // Check if set and post alternate phone without proper 10 digit

    if (!empty($_POST['billing_alternate_phone'])){
        if ( ! (preg_match('/^[0-9]{10}$/D', $_POST['billing_alternate_phone'] ))){
            wc_add_notice( "The alternate Phone should contain only 10 digits"  ,'error' );
        }
    }

}

/**
 * Update the user meta with field value
 **/
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['billing_alternate_phone']) update_user_meta( $user_id, '_alternate_phone_number', esc_attr($_POST['billing_alternate_phone']) );
    if ($user_id && $_POST['billing_article_title']) update_user_meta( $user_id, '_article_title', esc_attr($_POST['billing_article_title']) );
    if ($user_id && $_POST['billing_article_id']) update_user_meta( $user_id, '_article_id', esc_attr($_POST['billing_article_id']) );
}
/**
 * Update the order meta with field value
 **/
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 ($_POST['billing_alternate_phone']) update_post_meta( $order_id, '_alternate_phone_number', esc_attr($_POST['billing_alternate_phone']));
    if ($_POST['billing_article_title']) update_post_meta( $order_id, '_article_title', esc_attr($_POST['billing_article_title']));
    if ($_POST['billing_article_id']) update_post_meta( $order_id, '_article_id', esc_attr($_POST['billing_article_id']));
    if ($_POST['billing_first_name']) update_post_meta( $order_id, '_article_author_name', esc_attr($_POST['billing_first_name']));
}
/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {

    $keys[] = 'Name of Corresponding Author';
    $keys[] = 'Article Title';
    $keys[] = 'Article Submission Id';
    $keys[] = 'Alternate Phone Number';
    return $keys;
}

/**
 * Display Order data in thankyou page
 **/
function cloudways_display_order_data( $order_id ){  ?>
    <h2><?php _e( 'Article Information' ); ?></h2>
    <table class="shop_table shop_table_responsive additional_info">
        <tbody>
        <tr>
            <th><?php _e( 'Author Name:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_article_author_name', true ); ?></td>
        </tr>
        <tr>
            <th><?php _e( 'Article Title:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_article_title', true ); ?></td>
        </tr>
        <tr>
            <th><?php _e( 'Article Submission ID:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_article_id', true ); ?></td>
        </tr>
        </tbody>
    </table>
<?php }
add_action( 'woocommerce_thankyou', 'cloudways_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'cloudways_display_order_data', 20 );

/**
 * @param $order
 * Display order data in admin backend
 */

function cloudways_display_order_data_in_admin( $order ){  ?>
    

' . __( 'Author Name' ) . ':' . get_post_meta( $order->id, '_article_author_name', true ) . '

'; echo '

' . __( 'Article Title' ) . ':' . get_post_meta( $order->id, '_article_title', true ) . '

'; echo '

' . __( 'Article Submission ID' ) . ':' . get_post_meta( $order->id, '_article_id', true ) . '

'; echo '

' . __( 'Alternate Phone Number' ) . ':' . get_post_meta( $order->id, '_alternate_phone_number', true ) . '

';?>
'_article_author_name', 'label' => __( 'Author Name' ), 'wrapper_class' => '_billing_first_name' ) ); ?> '_article_title', 'label' => __( 'Article Title' ), 'wrapper_class' => '_billing_article_title' ) ); ?> '_article_id', 'label' => __( 'Article Submission Id' ), 'wrapper_class' => '_billing_article_id' ) ); ?> '_alternate_phone_number', 'label' => __( 'Alternate Phone Number' ), 'wrapper_class' => '_billing__alternate_phone' ) ); ?>
</div> <?php } add_action( 'woocommerce_admin_order_data_after_order_details', 'cloudways_display_order_data_in_admin' ); /** * @param $post_id * @param $post * Update post meta with new data */ function cloudways_save_extra_details( $post_id, $post ){ update_post_meta( $post_id, '_article_author_name', wc_clean( $_POST[ '_article_author_name' ] ) ); update_post_meta( $post_id, '_article_title', wc_clean( $_POST[ '_article_title' ] ) ); update_post_meta( $post_id, '_article_id', wc_clean( $_POST[ '_article_id' ] ) ); update_post_meta( $post_id, '_alternate_phone_number', wc_clean( $_POST[ '_alternate_phone_number' ] ) ); } add_action( 'woocommerce_process_shop_order_meta', 'cloudways_save_extra_details', 45, 2 ); /** * @param $fields * @param $sent_to_admin * @param $order * @return mixed * Add meta informations into order email */ function cloudways_email_order_meta_fields( $fields, $sent_to_admin, $order ) { $fields['author_name'] = array( 'label' => __( 'Corresponding Author' ), 'value' => get_post_meta( $order->id, '_article_author_name', true ), ); $fields['article_title'] = array( 'label' => __( 'Article Title' ), 'value' => get_post_meta( $order->id, '_article_title', true ), ); $fields['article_submission_id'] = array( 'label' => __( 'Article Submission ID' ), 'value' => get_post_meta( $order->id, '_article_id', true ), ); $fields['alternate_phone_number'] = array( 'label' => __( 'Alternate Phone Number' ), 'value' => get_post_meta( $order->id, '_alternate_phone_number', true ), ); return $fields; } add_filter('woocommerce_email_order_meta_fields', 'cloudways_email_order_meta_fields', 10, 3 );
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.