I have some additional fields in my checkout for asking questions from the customer about certain things regarding their order. Most of these are text input fields but there's one Select menu asking them how they heard about my clients site. I've been unable to figure out how to display the value in the meta area of the order for the selected option. Here's how I'm generating that select drop down in my functions.php file from my theme.
woocommerce_form_field( 'aba_hear', array(
'type' => 'select',
'required' => 'true',
'class' => array('hear-class form-row-wide'),
'label' => __('How did You Hear About Us?'),
'options' => array( // options for <select> or <input type="radio" />
'' => 'Please select', // empty values means that field is not selected
'Instagram' => 'Instagram',
'Facebook' => 'Facebook',
'Yelp' => 'Yelp',
'Other' => 'Other',
)
), $checkout->get_value( 'aba_hear' ) );
Now I go add a function to update the order meta values:
add_action( 'woocommerce_checkout_update_order_meta', 'aba_checkout_field_update_order_meta' );
function aba_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['aba_hear'] ) ) {
update_post_meta( $order_id, 'How did You Hear About Us?', sanitize_text_field( $_POST['aba_hear'] ) );
}
}
And finally display the value on the order page:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('How did You Hear About Us?').':</strong> ' . get_post_meta( $order->id, 'Is this a Gift?', true ) . '</p>';
}
Lastly here's how the select menu's code appears at checkout:
<p class="form-row hear-class form-row-wide validate-required" id="aba_hear_field" data-priority="">
<label for="aba_hear" class="">How did You Hear About Us? <abbr class="required" title="required">*</abbr></label>
<span class="woocommerce-input-wrapper">
<select name="aba_hear" id="aba_hear" class="select " data-allow_clear="true" data-placeholder="Please select">
<option value="" selected='selected'>Please select</option>
<option value="Instagram" >Instagram</option>
<option value="Facebook" >Facebook</option>
<option value="Yelp" >Yelp</option>
<option value="Other" >Other</option>
</select>
</span>
</p>
Now, this works fine for text input fields but not select menus. How can I change this to make it work so I can display the resulting data?
There are some mistakes in some of your functions… You need to use the same checkout field key as meta key in the following functions:
In the 2nd function, you use another hook and save custom field as user meta data too:
add_action( 'woocommerce_checkout_create_order', 'aba_checkout_field_update_order_meta' );
function aba_checkout_field_update_order_meta( $order ) {
if ( isset($_POST['aba_hear']) && ! empty($_POST['aba_hear']) ) {
$order->update_meta_data( '_aba_hear', sanitize_text_field( $_POST['aba_hear'] ) );
// Update user data
if( $order->get_user_id() > 0 ) {
update_user_meta( $order->get_user_id(), 'aba_hear', true );
}
}
}
In the 3rd function use this:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta( $order ){
$value = $order->get_meta( '_aba_hear' );
if ( ! empty($value) ) {
echo '<p><strong>'.__('How did You Hear About Us?').':</strong> ' . $value . '</p>';
}
}
It should better work now.
Notes:
Why saving this checkout field also as custom user meta data?
Because in your first function you have
$checkout->get_value( 'aba_hear' )
that will display the selected value on from customer last order in this custom checkout field. The value is read from user meta 'aba_hear'.