Added some text fields to add for whom we are sending the gift by adding their name and personal message as well.For Personal message it is working fine If i add To field and click on proceed to checkout and in checkout page if i place order in admin panel orders section it is not displaying the To field name which i have added in cart page.It is displaying the heading as TO and displaying blank not getting the name which i have entered.here is the cde which i ave written in funcions.php file

    // Add the order_comments field to the cart
 add_action( 'woocommerce_cart_collaterals', 'order_comments_custom_cart_field' );
 function order_comments_custom_cart_field() {
?>
<div class="customer_notes_on_cart" style="clear:both;">
<?php
 woocommerce_form_field('to_notes_text', array(
'placeholder'   => __('To'),
'class' => array('form-row-last'),
'clear' => true,
 ), ''); 
 ?></div><?php
 }
 // PHP: Remove "(optional)" from non required fields
 add_filter( 'woocommerce_form_field' , 
'remove_checkout_optional_fields_label', 10, 4 );
 function remove_checkout_optional_fields_label( $field, $key, $args, $value 
 ) {
// Only on cart page
if( is_cart() ) {
$optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// Process the checkout and overwriting the normal button
  add_action( 'woocommerce_proceed_to_checkout', 'change_proceed_to_checkout', 15 );
 function change_proceed_to_checkout() {
 remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
 ?>
 <form id="checkout_form" method="POST" action="<?php echo wc_get_checkout_url(); ?>">
<input type="hidden" name="to_notes" id="to_notes" value="">        
<button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
 </form>
 <?php
 }
 // Jquery script for cart and checkout pages
 add_action('wp_footer', 'customer_notes_jquery' );
 function customer_notes_jquery() {
 ?>
 <script>
 jQuery(function($) {
<?php // For cart
    if( is_cart() ) : ?>            
    $('#to_notes_text').on( 'blur', function(){
        $('#to_notes').val($(this).val());
    });

<?php // For checkout
    elseif( is_checkout() && ! is_wc_endpoint_url() ) : ?>
    $('#to_comments' ).val("<?php echo sanitize_text_field($_POST['to_notes']); ?>");           
<?php endif; ?>
 });
 </script>
 <?php
 }
 /**
 * 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 ) {
 ?>
 <?php
 if ( ! empty( $_POST['to_notes_text'] ) ) {
update_post_meta( $order_id, 'To', sanitize_text_field( 
 $_POST['to_notes_text'] ) );
  }
  }
  /**
  * 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){
?>
<?php
echo '<p><strong>'.__('TO').':</strong> ' . get_post_meta( $order->id, 'TO', true ) . '</p>';
     }

put on hold as off-topic by Krzysiek Dróżdż, shanebp, Jacob Peattie, rudtek, bosco Dec 12 at 7:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Anyone who can help me to get it out done – tester Dec 3 at 6:37
up vote 0 down vote accepted

Solved by adding this code

/**
 * 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 ) {
 ?>
 <?php
 if ( ! empty( $_POST['from_comments'] ) ) {
update_post_meta( $order_id, 'FROM', sanitize_text_field( 
 $_POST['from_comments'] 
 ) );
 }   
 }
 /**
 * Update the order meta with field value
 **/ 
 add_action( 'woocommerce_checkout_update_order_meta', 
'my_customs_checkout_field_update_order_meta' );
 function my_customs_checkout_field_update_order_meta( $order_id ) {
  ?>
 <?php
 if ( ! empty( $_POST['to_comments'] ) ) {
update_post_meta( $order_id, 'TO', sanitize_text_field( 
 $_POST['to_comments'] ) 
 );
 }    
 }
 /**
 * 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){
  ?>
  <?php
  echo '<p><strong>'.__('FROM').':</strong> ' . get_post_meta( $order->id, 'FROM', true 
   ) . '</p>';
   echo '<p><strong>'.__('TO').':</strong> ' . get_post_meta( $order->id, 'TO', true ) 
   . '</p>';
   }
   /**
   * Add the field to the checkout
   */
   add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

    function my_custom_checkout_field( $checkout ) {
     ?>
    <?php
    echo '<div id="my_custom_checkout_field"><h2>' . __('TO ') . '</h2>';
    woocommerce_form_field( 'from_comments', array(
   'type'          => 'text',
   'class'         => array('my-field-class form-row-wide'),
    'placeholder'   => __('FROM'),
     ), $checkout->get_value( 'from_comments' ));

   woocommerce_form_field( 'to_comments', array(
'type'          => 'text',
'class'         => array('my-field-class form-row-wide'),
'placeholder'   => __('TO'),
), $checkout->get_value( 'to_comments' ));
   echo '</div>';
    }

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