0

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. dropdown

Custom Field Value

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.

1
  • Take a look at the code of woocommerce_form_field, you'll see that the array key is used as value for the select option, so it's going to be saved, ergo structure your array accordingly. Dec 26, 2016 at 13:45

2 Answers 2

0

Your options should have a key => value structure if you want it to return something other than the key number:

'options'     => array(
    'option_a' => __('option a', 'woocommerce' ),
    'option_b' => __('option b', 'woocommerce' )
    )
);
0

Thanks everyone! It's solved.

'options'       => array(     
                 $vessel_one => __($vessel_one, 'woocommerce' ),
                 $vessel_two => __($vessel_two, 'woocommerce' ),
                 $vessel_three => __($vessel_three, 'woocommerce' ),
                 $vessel_four => __($vessel_four, 'woocommerce' ),
                 $vessel_five => __($vessel_five, 'woocommerce' )
                )

With this it will automatically show the selected value from the user custom meta in the custom field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.