It’s possible to add extra custom fields to the checkout process if you want to collect more information from the customer. In this example, extra fields for “student name” and “student year” are being added. There are a number of steps involved. First, the fields need to be created; then the data entry fields are displayed on the checkout form. The fields need to be updated as the new data is entered, and then displayed in the admin area on the order form so the information can be viewed later. Finally, the information is added to the customer’s email, so that they can check what details were supplied.
/**
* Add extra checkout fields
*/
add_filter('woocommerce_checkout_fields', 'cc_add_extra_checkout_fields');
function cc_add_extra_checkout_fields($fields) {
$fields['extra_fields'] = array(
'student_name' => array(
'type' => 'text',
'required' => true,
'label' => __('Student first and last name'),
'placeholder' => '',
'class' => array('student-name')
),
'student_year' => array(
'type' => 'text',
'required' => true,
'label' => __('Year'),
'placeholder' => '',
'class' => array('student-year')
),
);
return $fields;
}
// display the extra field on the checkout form
add_action('woocommerce_checkout_after_customer_details' ,'cc_extra_checkout_fields');
function cc_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<?php
// because of this foreach, everything added to the array in the previous function will display
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php }
/**
* Update the value given in custom fields
*/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
function custom_checkout_field_update_order_meta($order_id) {
if (!empty($_POST['student_name'])) {
update_post_meta($order_id, 'student_name',sanitize_text_field($_POST['student_name']));
}
if (!empty($_POST['student_year'])) {
update_post_meta($order_id, 'student_year',sanitize_text_field($_POST['student_year']));
}
}
/**
* Display field values on the order edit page
*/
add_action('woocommerce_admin_order_data_after_billing_address', 'cc_custom_checkout_field_display_admin_order_meta', 10, 1);
function cc_custom_checkout_field_display_admin_order_meta($order) {
echo '<p><strong>'.__('Student name').':</strong> ' . get_post_meta( $order->get_id(), 'student_name', true ) . '</p>';
echo '<p><strong>'.__('Student year').':</strong> ' . get_post_meta( $order->get_id(), 'student_year', true ) . '</p>';
}
/**
* Add a custom field (in an order) to the emails
*/
add_filter('woocommerce_email_order_meta_fields', 'cc_email_order_meta_fields', 10, 3);
function cc_email_order_meta_fields($fields, $sent_to_admin, $order) {
$fields['student_name'] = array(
'label' => __('Student name'),
'value' => get_post_meta($order->get_id(), 'student_name', true),
);
$fields['student_year'] = array(
'label' => __('Student year'),
'value' => get_post_meta($order->get_id(), 'student_year', true),
);
return $fields;
}
View Raw
Code ID: 9924