Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @description This file will be responsible for match user location and Dealer location based on there longitude and latitude
- * @author Luxury Buys Today
- *
- */
- /**
- * @description this function will return you nearest dealers id
- * @param array $coordinate
- * @param int $limit
- * @param int $max_distance
- * @param int | null $cat
- * @return Object
- * @to-do Documentation to be improved
- * @to-do Code need to be much cleaner
- */
- function get_matched_dealer($coordinate=array(),$cat=null,$limit= 20,$max_distance=50){
- // var_dump($cat);
- global $wpdb;
- $return_array=new stdClass(); //
- $user_latitude = $coordinate['latitude'] ? $coordinate['latitude'] : '0';
- $user_longitude = $coordinate['longitude'] ? $coordinate['longitude'] : '0';
- /**
- * Having some problem to query from certain category, need to fix it but meanwhile going to filter by cat after retrieve from database, and let's multiply it by 8 to
- * @to-do filter part have to be rewritten
- */
- // $user_latitude = 36.0979230;
- // $user_longitude = -80.1036380;
- $dealers=$wpdb->get_results(
- "SELECT DISTINCT post_id as dealer_id,address, ( 3959 * acos( cos( radians($user_latitude) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($user_longitude) ) + sin( radians($user_latitude) ) * sin( radians( lat ) ) ) ) AS distance FROM
- (
- SELECT a.post_id, a.meta_value as lat, b.meta_value as lng, c.meta_value as address
- FROM
- lbt_postmeta as a,
- lbt_postmeta as b,
- lbt_postmeta as c,
- lbt_term_relationships as d
- WHERE a.meta_key = 'geo_latitude'
- AND b.meta_key= 'geo_longitude'
- AND c.meta_key= 'geo_address'
- AND a.post_id = b.post_id
- and a.post_id = c.post_id
- AND a.post_id = d.object_id
- AND d.term_taxonomy_id = $cat
- )
- as f_table HAVING distance < $max_distance ORDER BY distance LIMIT 0, $limit"
- ); // end of get_result method
- if($dealers) {
- $return_array->dealers=array();
- $return_array->dealer_ids=array();
- foreach($dealers as $dealer){
- $return_array->dealer_ids[]=$dealer->dealer_id;
- $return_array->dealers[]=$dealer;
- }
- //$return_array->dealers_id=$dealers_id;
- }
- else {
- // echo "no matching dealers";
- return;
- }
- return $return_array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement