I'm trying to calculate shipping rate based on distance by KM on Woocommerce by sending the total distance to an external API, so, for this purpose I have this function inside my own shipping plugin:
public function calculate_shipping( $packages = array() ) {
wp_parse_str( $_POST['post_data'], $post_data );
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, $this->url_api.$post_data["distancia_billing"]);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$datos = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($datos, true);
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => isset($jsonArrayResponse["precio"]) ? $jsonArrayResponse["precio"] : '10.00',
'calc_tax' => 'per_order'
);
$this->add_rate( $rate );
}
Works well and show the correct price during the checkout proccess, but when the user finish the checkout proccess and I check the order on my Woocommerce panel always shows the default value (10.00) instead the calculated value on the API.
I'm missing anything on my function?
Thanks