0

I have a few custom fields within my WooCommerce setup, however I am trying to get them to display on the order view.

This is my code (one custom field for the sake of it):

add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

echo '<div id="my_custom_checkout_field"><h3>'.__('Extra Information').'</h3>';

woocommerce_form_field( 'date_of_birth', array(
    'type'          => 'select',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Date of Birth'),
    'placeholder'       => __('Enter something'),
'required'  => true,
'options'           => array(
        '1995' => __('1995', 'woocommerce' ),
        '1994' => __('1994', 'woocommerce' )
    )
 ), 
  $checkout->get_value( 'date_of_birth' ));

I thought this below would do the job on adding it to the order view, however it does not.. or I am completely wrong and the custom field is not saving?

 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 ($_POST['date_of_birth']) update_post_meta( $order_id, 'Extra Information', esc_attr($_POST['date_of_birth']));

I then looked through stackoverflow and found some more information and tried this:

function my_custom_checkout_field1($order){
echo "<p><strong>Date of Birth:</strong> " . $order->order_custom_fields['_date_of_birth'][0] . "</p>";
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field1', 10, 1 );

That does add a "Date of Birth:" to the order view, but unfortunately it is empty afterwards. I tried both _date_of_birth and date_of_birth, apparently it needed the extra _ infront, I got that answer from here: Show custom field on order in woocommerce

I have tried to look inside the database, but can not find the extra details on an order displayed anywhere (no regular details either except order id's).

Can anyone tell me what I am doing wrong and what I can do to make the field show up on the order view?

Thanks

4

1 回答 1

0

我不知道,但是这个:

'Extra Information'

这里:

update_post_meta($post_id, $meta_key, $meta_value, $prev_value);

您确定“额外信息”是 $meta_key 吗?不是“date_of_birth”吗?

于 2013-06-21T19:28:55.073 回答