Getting a “successful submission” green box when response has returned errors.
-
So, I am trying to engineer the situations that occur when a request is successful or not. By a successful request, I simply mean the information has passed any additional validation at the API and the details are accepted. In this case I wish to attach the returned Quote to the email sent to the user, but if the validations fail I want to inform the user and allow them to resubmit.
public function alter_response($response, $results){ $xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); $array = json_decode($json,TRUE); $errs = array(); if(array_key_exists('errors', $array['Response'])) { foreach ($array['Response']['errors'] as $error) $errs[] = $error['reason_description']; } if (sizeof($errs) > 0) { $results['success'] = "false"; $results['errors'] = $errs; $results['message'] = "There was a problem with some fields."; } else { $results['success'] = "true"; $results['message'] = "Your submission was a success"; $results['attach'] = $response['RSFullApplicationResponse']['Quote']['result']; } error_log("Response:" . print_r($array, TRUE)); error_log("Errors:" . print_r($errs, TRUE)); error_log("Results:" . print_r($results, TRUE)); }
tl;dr
Creates an array from the XML response, checks for errors and adds them to the separate errors array if they exist, then checks to see if the errors array is empty; if it is then the submission is a success, if not then the submission is a failure.As you can see, I am printing out the results array at the end. Even on the cases when the array looks like this:
Results:Array
(
[success] => false
[errors] => Array
(
[0] => Internal Address Error ( Insufficient Address History: Must be at least 36 months).
)[attach] =>
[message] => There was a problem with some fields.
)because there was an error, I still receive a “Your message was sent successfully. Thanks.” with a green box.
I noticed in the 3rd-parties/mailchimp example it says:
@param &$results the callback return results (passed by reference since function can’t return a value; also must be “constructed by reference”; see plugin)but when I pass by reference, I get the following message:
“PHP Warning: Parameter 2 to Cf73rdParty_RateSetterCallbacks::alter_response() expected to be a reference, value given in /var/www/wp/wp-includes/plugin.php on line 524”Is this this issue, and if so how do I fix it? I can’t find this information anywhere.
Thanks in advance,
Daniel
- The topic ‘Getting a “successful submission” green box when response has returned errors.’ is closed to new replies.