Site icon Hip-Hop Website Design and Development

Learn how to save one API response to WooCommerce order meta information

Have one customized checkout subject e mail sort that validates if the e-mail exists or not in a distant server. I despatched JSON information to the distant server with this subject.

If e mail exists JSON response sometimes:

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

The place “token” worth adjustments per request.

I wish to save the token how metadata contained in the order, after test what response code = 200

Utilizing $order->update_meta_data( 'received_token_key', $token ); not work alone, I do know what I’ve one huge error, however I am unable to discover this one.

Perhaps utilizing one other WooCommerce hook?
Perhaps utilizing wc_session?
Perhaps save to a different place and recollect it?

I want what earlier than checkout happens, the e-mail from customized subject to be validated, if not, wc_add_notice($response_message, 'error'); might be triggered.

         // begin right here
   add_action( 'woocommerce_checkout_process', (custom_checkout_field_process_valid' );

   operate custom_checkout_field_process_valid($order_id) {

        $url = 'https://remoteserver.com/api/checking-email-registered-user.json';
        $physique = array("e mail"=>($_POST['email_pasajero']),); //from customized subject
        $response2 = wp_remote_post( $url, 
            array(
                'headers'   => array('Content material-Kind' => 'utility/json; charset=utf-8'),
                'methodology'    => 'POST',
                'timeout' => 75,                    
                'physique'      => json_encode($physique),));

         // test 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 wish to save $token or its worth, on this step, how metadata for later use
       // perhaps straight from the response?
        // $order->update_meta_data( 'token_received_key', $token ); not work
    }

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