Ok so now im as far as to getting “meters” returned after customer filled in street/city in page 1 of the form.
Only thing is i only manage to get the result into an “html-block” with code underneath (by now plugin). I would like to get the result into the quantity field of a product, but parsing result to “value” of relevant field id doenst seem to work.
example:
terramilieu.nl/test
My code:
function getDrivingDistance($startstreet, $startcity, $endstreet, $endcity)
{
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".urlencode($startstreet)."+".urlencode($startcity)."&destinations=".urlencode($endstreet)."+".urlencode($endcity)."&mode=driving&key=AIzaSyDtNDZRDEy2b8FLchNY4I2Okl1sLlFEIDw";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
//$response_a = json_decode($response, true);
$json = file_get_contents($url); // get the data from Google Maps API
$result = json_decode($json, true); // convert it from JSON to php array
$dist = $result['rows'][0]['elements'][0]['distance']['text'];
$meters = $result['rows'][0]['elements'][0]['distance']['value'];
$time = $result['rows'][0]['elements'][0]['duration']['text'];
return array('distance' => $dist, 'meters'=> $meters, 'time' => $time);
}
function gdmDist($street, $city){
$result = getDrivingDistance('Industrieweg 16', 'Vught', $street, $city);
return ($result['meters']);
}
add_filter( 'gform_pre_render_6', 'populate_html' );
function populate_html( $form ) {
//this is a 2-page form with the data from page one being displayed in an html field on page 2
$current_page =GFFormDisplay::get_current_page( $form['id'] );
if ( $current_page == 2 ) {
foreach ( $form['fields'] as &$field ) {
//gather form data to save into html field (id 6 on my form), exclude page break
$field_data = rgpost('input_' . $field->id );
if ( $field->id == 49 && $field->type != 'page' )
{
$html_content_49 = $field_data;
}
elseif ( $field->id == 50 )
{
$html_content_50 = $field_data;
}
}
//loop back through form fields to get html fields that we are populating with the data gathered above
foreach( $form['fields'] as &$field ) {
//get html field
if ( $field->id == 51 ) {
//set the field content to the html
$field->content = $html_content_49;
}
if ( $field->id == 58 ) {
//set the field content to the html
$field->content = $html_content_50;
}
if ( $field->id == 60 ) {
$fieldval_address = $html_content_49;
$fieldval_city = $html_content_50;
$result_end = getDrivingDistance('Industrieweg 16', 'Vught', $fieldval_address, $fieldval_city);
//$result = gdmDist($fieldval_address, $fieldval_city);
//set the field content to the html
$field->content = $result_end['meters'];
}
}
}
//return altered form so changes are displayed
return $form;
}