0

thanks for your help in advance.

I have a wordpress site running woocommerence and I have it set up as a multi site.

Basically I run a business which delivers McDonald's and KFC to customers. Each area has it's own site which is part of the multisite i've set up.

The main site which functions to give the user the link to the multisite which delivers in their area.

I need to run a simple code on the main site so when the user types in their postcode or village then it takes them to the right part of the multisite for them.

for example www.foodtoyourdoor.co.uk is the main site and the user would type in their postcode or say village "Durham" when the user hits enter it takes them directly to durham.foodtoyourdoor.co.uk.

i've been looking at yourtube for weeks and cant find anything to help me.

  • Could you post what you tried ? – Sylvain Martin Aug 10 '15 at 11:19
  • Please share with us some code that you already tried. Also this question is far too broad to yield a good answer with the details you have provided. Were do for example the names for the cities come from? Is it in a database, or is anything possible? Make your question as narrow as possible in order to get high quality answers. – Luuklag Aug 10 '15 at 11:20
  • i haven't tried anything yet. I dont have a database set up but i do have an excel sheet with all the postcodes so could import this some how i guess – David Turner Aug 10 '15 at 14:10

1 Answers1

0

The first step you need to do is store the longitude and latitude of the sites in the database. When you've done that you can convert the posted address of a client to longitude and latitude as well:

function getLongitudeAndLatitude($address) {
    $url = 'http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&language=nl&address=' . str_replace(' ','+', $address);
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);  
    $obj = simplexml_load_string($response);

    $lat = null;
    $lon = null;

    if ($obj) {
        $obj = @$obj->result;
        $lat = (@$obj->geometry->location->lat.'');
        $lon = (@$obj->geometry->location->lng.'');
    }
    return array( 'latitude' => $lat, 'longitude' => $lon,);
}

With this data you then can calculate the distance between the client and the sites. To do this you can create a routine in mysql

CREATE DEFINER=`user`@`localhost` FUNCTION `haversine`(`lat1` FLOAT, `lon1` FLOAT, `lat2` FLOAT, `lon2` FLOAT) RETURNS float
    NO SQL
    DETERMINISTIC
    COMMENT 'Returns the distance in degrees on the Earth             between two known points of latitude and longitude'
BEGIN
    RETURN 111.045*DEGREES(ACOS(
              COS(RADIANS(lat1)) *
              COS(RADIANS(lat2)) *
              COS(RADIANS(lon2) - RADIANS(lon1)) +
              SIN(RADIANS(lat1)) * SIN(RADIANS(lat2))
            ));
END

With this routine you then can create a SQL that returns the nearest site

$user_location = getLongitudeAndLatitude($_POST['txt_address']);
if (isset($user_location['latitude']) && isset($user_location['longitude'])) {
    $stmt = $pdo->prepare('SELECT *, haversine(:lat1, :lon1, site.latitude, site.longitude) as distance FROM site ORDER BY distance LIMIT 1');
    $stmt->execute([':lat1' => $user_location['latitude'], ':lon1' => $user_location['longitude']);
    $res = $stmt->fetch(PDO::FETCH_ASSOC);
    var_dump($res);
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • Thanks DarkBee but i don't have a clue of anything you have put. I'm not exactly ok with this kind of programming. – David Turner Aug 10 '15 at 14:11
  • This is stackoverflow. We are here to help you with the problems you encounter, not for writing full projects/solutions – DarkBee Aug 10 '15 at 17:03