Have one custom checkout field email type that validates if the email exists or not in a remote server. I sent JSON data to the remote server with this field.

If email exists JSON response typically:

    "success": "ok",
    "responseCode": "200",
    "token": "I0b6uSNelqwGv+sCHpTV6LYI08hxcwoBIMVHh5mKVx+fkRFscUKbvUBF8+rl0vACgMImcffCzLsf571KLQVkSYcvdUfdwsu8mrBQ8mBw5J1LZtFs/7PwXerjNS2HjBAB0tc+RqZhYJ9Ne1efn7fq7Df4kAQ+QO7CjBKUwp8k7pFYslLMcWuxfb+KKsotZP82gkk5oKZEeh30a6K2I7dAxRj+B3RqyqND/uRO1uqQ1IA="

Where "token" value changes per request.

I want to save the token how metadata inside the order, after check what response code = 200

Using $order->update_meta_data( 'received_token_key', $token ); not work alone, I know what I have one big error, but I can't find this one.

Maybe using another WooCommerce hook? Maybe using wc_session? Maybe save to another place and recall it?

I need what before checkout occurs, the email from custom field to be validated, if not, wc_add_notice($response_message, 'error'); will be triggered.

         // start here
   add_action( 'woocommerce_checkout_process', (custom_checkout_field_process_valid' );

   function custom_checkout_field_process_valid($order_id) {

        $url = 'https://remoteserver.com/api/checking-email-registered-user.json';
        $body = array("email"=>($_POST['email_pasajero']),); //from custom field
        $response2 = wp_remote_post( $url, 
                'headers'   => array('Content-Type' => 'application/json; charset=utf-8'),
                'method'    => 'POST',
                'timeout' => 75,                    
                'body'      => json_encode($body),));

         // check and use the response
$response_code       = wp_remote_retrieve_response_code( $response2 );
$response_message = wp_remote_retrieve_response_message( $response2 );
$bodyarray = json_decode( wp_remote_retrieve_body( $response2 ));
$token = $bodyarray["token"]; 

    if ($response_code == 200)  {
        // I want to save $token or its value, in this step, how metadata for later use
       // maybe directly from the response?
        // $order->update_meta_data( 'token_received_key', $token ); not work

            else {
         wc_add_notice($response_message, 'error');
                return $response_code ;

1 Answer 1


You're very close.

$order->update_meta_data() will update the meta value inside the $order object, but it will not store the change in the database. To save changes to a WooCommerce CRUD object you need to run the save() method:

if ( $response_code == 200 )  {
    $order->update_meta_data( 'token_received_key', $token );
  • Thanks, @jacob, but this does not work, give me internal server error how exception. when placing the order. When i set this how test sample: if ($response_code == 200) { wc_add_notice( $token , 'error' );} the error message show the token, any idea?
    – StarLine
    Oct 16, 2019 at 17:59
  • What is the error? Oct 16, 2019 at 23:54
  • show the message x Internal server error after place the order
    – StarLine
    Oct 18, 2019 at 19:27
  • didn't work for me either
    – Saud Khan
    Oct 27, 2020 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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