I've had the following code to add a custom order field in the woocommerce checkout:
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
)
);
return $fields;
}
Then, followed by the below code to 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(!empty($_POST['billing_reason_for_purchase'])) {
update_post_meta($order_id,'Reason for purchase',sanitize_text_field($_POST['billing_reason_for_purchase']));
}
}
Next, display the field on the order edit page:
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>'.__('Reason for purchase').':<strong>'.get_post_meta($order->id, 'Reason for purchase',true).'</p>';
echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta(get_the_ID(), 'Reason for purchase',true).'</p>';
}
The problem I'm having is if I created a dummy order with choosing "academic" as the reason for purchase, I'd get "option_2" instead of "academic" in the order edit page.
Please help point me in the right direction.