Skip Navigation

[Gelöst] Capture the distance between two points and save to custom field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I have two address fields, one in a parent post type and one in a child post type. When a new child post is created, I would like to calculate the distance between the two addresses and save that distance in another custom field in the child post type.

Solution: You can use the cred_submit_complete hook to trigger custom code with the shortcode toolset-maps-distance-value to calculate the distance between two addresses. Use the update_post_meta function to store that distance in another custom field.

add_action('cred_submit_complete', 'tssupp_calc_parent_child_distance',10,2);
function tssupp_calc_parent_child_distance($post_id, $form_data)
{
  $forms = array(123, 456);
  $relationship_slug = 'book-chapter';
  $parent_address_slug = 'book-address-1';
  $child_address_slug = 'chapter-address-1';
  $child_distance_slug = 'chapter-single-line-1';
  $unit = 'km';
  $decimals = 2;
 
  // if a specific form
  if ( in_array( $form_data['id'], $forms ) )
  {
    $related_post = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
    if( $related_post ) {
      $child_location = get_post_meta( $post_id, 'wpcf-'.$child_address_slug, true );
      $parent_location = get_post_meta( $related_post, 'wpcf-'.$parent_address_slug, true );
      if( $child_location && $parent_location ) {
        $distance = do_shortcode("[toolset-maps-distance-value location='" . $child_location . "' postmeta='wpcf-". $parent_address_slug . "' postmeta_id='" . $related_post . "' unit='" . $unit . "' decimals='" . $decimals . "']");
        update_post_meta( $post_id, 'wpcf-'.$child_distance_slug, $distance );
      }
    }
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#toolset-maps-distance-value

This support ticket is created vor 1 Jahr, 8 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Artikel
#2073479

Hey there guys ... quick question.

Id like to save the distance between to points as a toolset custom field when the user submits a CRED form - can this be done?

I thought I would try a generic field on the CRED form and try to get save the custom short-code that gets the data (in an earlier support request: https://toolset.com/forums/topic/maps-distance-filter-using-a-custom-field-as-the-location/) - this short-code works fine.
However the shortode has not yet got the data for the new location as its not yet been saved to the DB.

I have a form where I add a 'site' which is an address of on office.

There is a parent post type of (child) 'site' called 'EOI' (parent) that has another address.

I would like to save the distance between the two addresses when I save the NEW child post .

I am doing this because its easier for me to use the native fields where the data is stored on the post in a template with a lot of other locations.

Cheers!

#2073651

Hello, in this case I think you must use the Forms API to trigger some custom code when the child post Form is submitted. Your custom code would need to calculate the distance between the two address fields programmatically. Since there is no PHP API available for that distance calculation, you could use do_shortcode to leverage the existing toolset-maps-distance-value shortcode effectively from a PHP script. Calculate the distance, save that value into a custom field in the child post using update_post_meta, and you'll have access to that distance information later in the child post template.

Similar example using the cred_submit_complete hook:

add_action('cred_submit_complete', 'tssupp_calc_parent_child_distance',10,2);
function tssupp_calc_parent_child_distance($post_id, $form_data)
{
  $forms = array(123, 456);
  $relationship_slug = 'book-chapter';
  $parent_address_slug = 'book-address-1';
  $child_address_slug = 'chapter-address-1';
  $child_distance_slug = 'chapter-single-line-1';
  $unit = 'km';
  $decimals = 2;

  // if a specific form
  if ( in_array( $form_data['id'], $forms ) )
  {
    $related_post = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
    if( $related_post ) {
      $child_location = get_post_meta( $post_id, 'wpcf-'.$child_address_slug, true );
      $parent_location = get_post_meta( $related_post, 'wpcf-'.$parent_address_slug, true );
      if( $child_location && $parent_location ) {
        $distance = do_shortcode("[toolset-maps-distance-value location='" . $child_location . "' postmeta='wpcf-". $parent_address_slug . "' postmeta_id='" . $related_post . "' unit='" . $unit . "' decimals='" . $decimals . "']");
        update_post_meta( $post_id, 'wpcf-'.$child_distance_slug, $distance );
      }
    }
  }
}

You would replace 123, 456 with the numeric ID of the Form that creates child posts, or a comma-separated list of IDs if there are multiple Forms. You would also replace the other variables up at the top of the code - the post relationship slug, the parent post address field slug, the child post address field slug, the child post distance field slug (where the distance value will be saved), the distance unit, and number of decimal places for the distance calculation. Do not include the 'wpcf-' prefix in any of the address field slugs up at the top, the code below adds that prefix automatically as needed. This example saves the distance calculated only as a number, like 173.21, not including the units.

We have documentation for the relevant APIs available here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

And the distance calculation shortcode here:
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#toolset-maps-distance-value

#2074177

I didnt think about executing the short-code inside a function... that's going to be perfect .. testing now.

Thanks Christian.

#2074225

This code is slightly wrong I believe... just in case anyone else comes across it and has an issue this is how I solved it 🙂

This line is looking for the parent field but is referencing the 'post_id' of the child.

$parent_location = get_post_meta( $post_id, 'wpcf-'.$parent_address_slug, true );

Updating it to get use the captured id of the parent solved it 🙂 -> $related_post, which produced the post ID of the parent, then using it to get the address from the parent.

$parent_location = get_post_meta( $related_post, 'wpcf-'.$parent_address_slug, true );
#2074771

Oh yes, you are correct. I will update the code in my example for future reference, thanks for the update.

#2075569

My issue is resolved now. Thank you!

Awesome support ... awesome product.