php - Getting custom field data from cart page to checkout page in woocomerce

admin2025-06-05  1

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

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>';
     }
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

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>';
     }
Share Improve this question asked Dec 2, 2018 at 13:43 testertester 1112 silver badges7 bronze badges 1
  • Anyone who can help me to get it out done – tester Commented Dec 3, 2018 at 6:37
Add a comment  | 

1 Answer 1

Reset to default 0

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>';
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749132777a316638.html

最新回复(0)