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.
It happens because the value of the selected option (and so of
$_POST['billing_reason_for_purchase']
) is actually the key of the array (in your exampleoption_2
) and not the text related. In fact this is the option tag created:You're saving only the key of your array.
So you need to retrieve the array of options even on the
my_custom_checkout_field_display_admin_order_meta
function to get the proper text.You could copy the array in each function (but is not convenient to duplicate code), or put it in a global variable to be able to access it from anywhere (but then something else could change it), so instead you can use a function that returns the array:
And then use it where you need it: