• Resolved southafricanrob

    (@southafricanrob)


    Hello,
    Is there a way that I can retrieve the distance to use in a calculation? For example I would like to write a functions to display the estimated cost in fuel for someone to drive from their current location to the store.

    I know the distance is outputted with the underscore function <%= distance %> but not sure how I can retrieve that value?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there,

    The problem is that you can calculate the straight line distance, but that could be misleading for calculating fuel consumption because the driving distance could be massively different from the straight line.

    The distance is calculated in the wpsl_sql filter.

    But you can access the template variables using the wpsl_store_meta function like this if you want to do some manipulation to the values in PHP:

    add_filter( 'wpsl_store_meta', 'custom_store_meta', 10, 2 );
    function custom_store_meta( $store_meta, $store_id ) {
        $distance = $store_meta['distance'];
        // do stuff with the distance variable 
        // ...
        // and then assign it to a new metadata element in the array
        $store_meta['my_custom_distance'] = $distance
        return $store_meta;
    }

    You can do whatever you want in PHP inside of this function, and then you can output it in the template like <%= my_custom_distance %>

    I hope that helps!

    Thread Starter southafricanrob

    (@southafricanrob)

    Thanks – this is very helpful – I didn’t realise I could just set the variable from the store meta.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get the distance for use in a custom function’ is closed to new replies.