-
-
Save nickstewart95/20569f6dd274bed780eb7a6fa411d7cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( | |
'woocommerce_package_rates', | |
function ($rates) { | |
$customer = WC()->session->get('customer'); | |
$address = !empty($customer['shipping_address_1']) | |
? urlencode($customer['shipping_address_1']) | |
: false; | |
$city = !empty($customer['shipping_city']) | |
? urlencode($customer['shipping_city']) | |
: false; | |
$state = !empty($customer['shipping_state']) | |
? urlencode($customer['shipping_state']) | |
: false; | |
if (!$address || !$city || !$state) { | |
return $rates; | |
} | |
$url = "https://geocoding.geo.census.gov/geocoder/locations/address?street={$address}&city={$city}&state={$state}&benchmark=2020&format=json"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
$output = json_decode($output); | |
if (count($output->result->addressMatches) == 0) { | |
$rates = []; | |
return $rates; | |
} | |
$x = $output->result->addressMatches[0]->coordinates->x; | |
$y = $output->result->addressMatches[0]->coordinates->y; | |
$location_x = '-85.491302'; | |
$location_y = '38.36621792373851'; | |
$theta = $location_x - $x; | |
$distance = | |
sin(deg2rad($location_y)) * sin(deg2rad($y)) + | |
cos(deg2rad($location_y)) * | |
cos(deg2rad($y)) * | |
cos(deg2rad($theta)); | |
$distance = round(rad2deg(acos($distance)) * 60 * 1.1515); | |
if ($distance > 25) { | |
unset($rates['flat_rate:4']); | |
} | |
if ($distance > 100) { | |
unset($rates['local_pickup:5']); | |
} | |
return $rates; | |
}, | |
10, | |
2, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment