如果送货地址距离商店地址woocommerce <= 20km

时间:2017-10-27 10:34:06

标签: php wordpress

我正在尝试找到一个解决方案来创建一个条件,检查地址(账单或运费)是否在距商店地址20公里范围内,以便免费送货。

if ($checkout_fields['billing'] != $address) { //variable to save store address
   unset( $rates['free_shipping'] );
}

免费送货可以在woocommerce设置中实现是的,但我需要检查客户是否距离仓库20公里。

由于

1 个答案:

答案 0 :(得分:0)

我很确定你必须回到地理编码API来做这样的事情。一种可能性是回退到Google Maps API。前段时间我遇到了类似的问题并使用此代码段解决了这个问题:

    function getDistancebetween($clientadress, $shopadress){

    $clientadress = str_replace(' ','+',$clientadress);
    $shopadress = str_replace(' ','+',$shopadress);

    $geo_shop = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$clientadress.'&sensor=false');
    $json_shop = json_decode($geo_shop);

    $geo_adress = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$shopadress.'&sensor=false');
    $json_client = json_decode($geo_adress);

    $latitude_shop = $json_shop->results[0]->geometry->location->lat;
    $longitude_shop = $json_shop->results[0]->geometry->location->lng;
    $latitude_client = $json_client->results[0]->geometry->location->lat;
    $longitude_client = $json_client->results[0]->geometry->location->lng;

    $theta = $longitude_shop - $longitude_client;
    $dist = sin(deg2rad($latitude_shop)) * sin(deg2rad($latitude_client)) +  cos(deg2rad($latitude_shop)) * cos(deg2rad($latitude_client)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    return $dist * 60 * 1.1515 * 1.6093;

}

可以在https://web.stanford.edu/~eready/distance.html

找到R版本