Im in the midst of customizing WooCommerce checkout, adding a dropdown field which get user's meta keys and show as select option in the checkout page. I also managed to save the selected option after checkout into the order. However, the option is displaying as an integer instead of text in the custom field.
The following codes are my development, I hope someone can guide me out.
Retrieving user meta keys and display as dropdown options
add_action('woocommerce_after_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
$user_id = get_current_user_id();
$vessel_one = get_user_meta( $user_id, 'vessel_one', true );
$vessel_two = get_user_meta( $user_id, 'vessel_two', true );
$vessel_three = get_user_meta( $user_id, 'vessel_three', true );
$vessel_four = get_user_meta( $user_id, 'vessel_four', true );
$vessel_five = get_user_meta( $user_id, 'vessel_five', true );
woocommerce_form_field( 'order_vessel', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __( 'Select a Vessel' ),
'options' => array( $vessel_one, $vessel_two, $vessel_three, $vessel_four, $vessel_five )
),
$checkout->get_value( 'order_vessel' ));
}
Save the value into custom field on the order
add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
function wps_select_checkout_field_update_order_meta( $order_id ) {
if ($_POST['order_vessel']) update_post_meta( $order_id, 'order_vessel', esc_attr($_POST['order_vessel']));
}
I hope someone could show me how can I save the value as it is and not converted into an integer. Thank you very much in advance.
woocommerce_form_field
, you'll see that thearray
key
is used asvalue
for theselect
option
, so it's going to be saved, ergo structure yourarray
accordingly.