title: Save and display a custom field as order item meta data in WooCommerce
body: <p>In woocommerce, I have maisd wome customizations and I can add a custom text field in my products, Display the values in cart and checkout <em>(see the screenshots below)</em>.</p>
  
  <p>Text field in product page:</p>
  
  <p><a href="https://i.stack.imgur.com/izLzk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/izLzk.png" alt="enter image description here"></a></p>
  
  <p>Value in the cart:</p>
  
  <p><a href="https://i.stack.imgur.com/n7wyC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n7wyC.png" alt="enter image description here"></a></p>
  
  <p>Value in the checkout:</p>
  
  <p><a href="https://i.stack.imgur.com/S3zbD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S3zbD.png" alt="enter image description here"></a></p>
  
  <p>But I can not get it to appear in the purchase details or in the administration section  <em>(see the screenshots below)</em>.</p>
  
  <p>Checkout details without the value:</p>
  
  <p><a href="https://i.stack.imgur.com/QfanH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QfanH.png" alt="enter image description here"></a></p>
  
  <p>Administration orders without the value:</p>
  
  <p><a href="https://i.stack.imgur.com/eLl9Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eLl9Y.png" alt="enter image description here"></a></p>
  
  <p>In my code below, could someone tell what I am doing wrong?</p>
  
  <p>The code that I use in my <code>functions.php</code> file:</p>
  
  <pre><code>// Add the field to the product
  add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
  
  function my_custom_checkout_field() {
  global $product;
  $id = $product-&gt;get_id();
  
  
  // Get the field name of InputText1
  $InputText1Name = get_post_meta($id, 'InputText1', true);
  
   if ((!empty(get_post_meta($id, $InputText1, true)))){  
  echo '&lt;div id="InputText1"&gt;';
      echo '&lt;label&gt;'.__($InputText1Name).'&lt;/label&gt; &lt;input type="text" name="$InputText1V"&gt;';
      echo '&lt;/div&gt;';
   }
  }
  
  
  // Store custom field
  function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
  
  
  if( isset( $_REQUEST['$InputText1V'] ) ) {
      $cart_item_data[ '$InputText1V' ] = $_REQUEST['$InputText1V'];
      /* below statement make sure every add to cart action as unique line item */
      $cart_item_data['unique_key'] = md5( microtime().rand() );
  }
  return $cart_item_data;
  }
  add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
  
  // Render meta on cart and checkout
  function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ){
  
  
  // Get the product id inside the cart
  foreach( WC()-&gt;cart-&gt;get_cart() as $cart_item ){
  $product_id = $cart_item['product_id'];
  } 
  
  
  // Get the field name of InputText1
  $InputText1Name = get_post_meta($product_id, 'InputText1', true);
  
  $custom_items = array();
  /* Woo 2.4.2 updates */
  if( !empty( $cart_data ) ) {
      $custom_items = $cart_data;
  }
  if( isset( $cart_item['$InputText1V'] ) ) {
      $custom_items[] = array( "name" =&gt; $InputText1Name, "value" =&gt; $cart_item['$InputText1V'] );
  }
  return $custom_items;
  
  
  }
  add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
  
  
  // Display as order meta
  function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
  if( isset( $values['$InputText1V'] ) ) {
      wc_add_order_item_meta( $product_id, "$InputText1V", $values['$InputText1V'] );
  }
  }
  
  
  
  // Update the order meta with field value
  
  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['$InputText1V']) update_post_meta( $order_id, '$InputText1Name', esc_attr($_POST['$InputText1V']));
  }
  
  // Update the user meta with field value
  add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
  function my_custom_checkout_field_update_user_meta( $user_id ) {
  if ($user_id &amp;&amp; $_POST['$InputText1V']) update_user_meta( $user_id, '$InputText1V', esc_attr($_POST['$InputText1V']) );
  }
  
  add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );
  
  
  // Display field value on the order edit page
  
  add_action( 'woocommerce_admin_order_data_after_billing_address',     'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
  function my_custom_checkout_field_display_admin_order_meta( $order ){
  
  
  $order_id = method_exists( $order, 'get_id' ) ? $order-&gt;get_id() : $order-&gt;id;
  echo '&lt;p&gt;&lt;strong&gt;'.__($InputText1V).':&lt;/strong&gt; ' . get_post_meta( $order_id, $InputText1V, true ) . '&lt;/p&gt;';
  }
  
  
  function hide_order_item_meta_fields( $fields ) {
  $fields[] = 'current_view';
  $fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
  return $fields;
  }
  
  add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
  
  function order_meta_customized_display( $item_id, $item, $product ){
  
  
  $order_product_id = $item['product_id'];
  $field1name = get_post_meta($order_product_id, 'InputText1', true);
  
  echo'&lt;br&gt;';
  print_r($InputText1V);
  echo'&lt;br&gt;';
  
  
  echo  $field1name;
  echo ': ';
  
  }
  </code></pre>