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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…