2

In Woocommerce, I used Add a new custom checkout field before billing details in Woocommerce? from @LoicTheAztec that works perfectly.

I'm having trouble displaying the information in the admin order page.

Then I tried using Add order metadata to WooCommerce admin order overview, making some changes in the code to match with my needs:

// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){  ?>
    <div class="order_data_column">
        <h4><?php _e( 'Extra Details' ); ?></h4>
        <?php 
            echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_my_field_name', true ) . '</p>';?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );?>

Extra Details and Some field: appears, but the custom field value isn't displayed.

I'm still trying to figure out what I did wrong here. Any suggestions on where I should look?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
J. Frenk
  • 21
  • 3

1 Answers1

3

Update 3: Try the following lightly revisited code:

add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
function custom_checkout_fields_before_billing_details(){
    $domain = 'woocommerce';
    $checkout = WC()->checkout;

    echo '<div id="my_custom_checkout_field">';

    echo '<h3>' . __('My New Fields Section') . '</h3>';

    woocommerce_form_field( '_my_field_name', array(
        'type'          => 'text',
        'label'         => __('My 1st new field', $domain ),
        'placeholder'   => __('Please fill in "my 1st new field"', $domain ),
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true, // or false
        ), $checkout->get_value( '_my_field_name' ));

    echo '</div>';
}

// Custom checkout fields validation
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
function custom_checkout_field_process() {
    if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
        wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );
}

// Save custom checkout fields the data to the order
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
function custom_checkout_field_update_meta( $order, $data ){
    if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
        $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );
}

// Display the extra data in the order admin panel
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
function display_order_data_in_admin( $order ){
    if( $value = $order->get_meta( '_my_field_name' ) ) {
        echo '<div class="order_data_column">
        <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
        <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
        </div>';
    }
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install – J. Frenk Nov 12 '18 at 00:43