1

I will that the custome can update in the order the custom fileds (who was insert in the checkout page)

I have this code in the order-details.php Page but it show only the value in the input but he don't update with the new value

<?php
 global $post;


 if ( isset( $_POST['submit'] ) )
 {
 if( ! isset( $post ) ) {
 echo 'Error: Nichts ausgewählt';
 die();
 }
 else if( ! isset( $_POST['frontstrasse'] ) && ! empty( $_POST['frontstrasse'] ) ){
 echo 'Error: Strasse Not Set';
 die();
 }


 update_post_meta( $order->id, 'frontstrasse', sanitize_text_field( $_POST['Strasse'] ) );
 }


 $istrasse = get_post_meta($order->id, 'Strasse', true );


 echo print_r($istrasse);
 ?>
 <form method="post" action="">
 <input type='text' name='frontstrasse' value='<?php echo isset($istrasse) ? $istrasse : ''; ?>' />
 <input type='submit' value='save' />
 </form>
1
  • Your code is a mess. Please make sure code is readable and properly indended before posting it. Messy code is hard to debug, specially if almost all of it is off screen ;-) Dec 7, 2015 at 10:39

2 Answers 2

1

I came across this post looking for a solution for multiple resubmissions due to update_post_meta. Here's my solution after looking at these posts wordpress get_post_meta check if multiple values set Prevent processing data multiple times in Woocommerce thankyou hook :

<?php
global $post;
$post = $order_id;
if ( isset( $_POST['submit'] ) ){
  echo 'Update nicht';
} else 
 
 // Checking if this has already been done avoiding reload
if ( get_post_meta($order_id,"frontstrasse",true)!="" && get_post_meta($order_id,"fronthausnr",true)!="" {
return; // Exit if already processed
}

  update_post_meta( $order_id, 'Strasse', sanitize_text_field( $_POST['frontstrasse'] ) );
  update_post_meta( $order_id, 'Haus-Nr', sanitize_text_field( $_POST['fronthausnr'] ) );
}

$istrasse = get_post_meta($order->id, 'Strasse', true );
$ihausnr = get_post_meta($order->id, 'Haus-Nr', true );
?>

<form method="post" action="">
<label>Strasse</label><input type='text' name='frontstrasse' value='<?php echo $istrasse ?>' />
<label>Haus-Nr</label><input type='text' name='fronthausnr' value='<?php echo  $ihausnr ?>' />
<input type='submit' value='save' name='frontsubmit' />
</form>
    
0

This code works for me. Sorry that the code is not good readable.

<?php
global $post;
$post = $order_id;
if ( isset( $_POST['submit'] ) ){
  echo 'Update nicht';
} else  if ( ! empty( $_POST['frontstrasse'] ) ) {
  update_post_meta( $order_id, 'Strasse', sanitize_text_field( $_POST['frontstrasse'] ) );
  update_post_meta( $order_id, 'Haus-Nr', sanitize_text_field( $_POST['fronthausnr'] ) );
}
$istrasse = get_post_meta($order->id, 'Strasse', true );
$ihausnr = get_post_meta($order->id, 'Haus-Nr', true );
?>

<form method="post" action="">
<label>Strasse</label><input type='text' name='frontstrasse' value='<?php echo $istrasse ?>' />
<label>Haus-Nr</label><input type='text' name='fronthausnr' value='<?php echo  $ihausnr ?>' />
<input type='submit' value='save' name='frontsubmit' />
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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