Custom WooCommerce shipping Method and distance rate value issue Custom WooCommerce shipping Method and distance rate value issue wordpress wordpress

Custom WooCommerce shipping Method and distance rate value issue


You need to set the cost to a WC_Session variable to avoid loosing the distance calculated cost value, so your function code should be:

            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);                                if( isset($jsonArrayResponse["precio"]) && ! empty($jsonArrayResponse["precio"]) && isset(WC()->session) ) {                    WC()->session->set('shipping_distance_cost', floatval($jsonArrayResponse["precio"]) );                }                $this->add_rate( array(                    'id'       => $this->id,                    'label'    => $this->title,                    'cost'     => WC()->session->__isset('shipping_distance_cost') ? WC()->session->get('shipping_distance_cost') : '10.00',                    'calc_tax' => 'per_order'                ) );            }

But you should need to remove that session variable when order has been created, so outside your shipping class code, add the following:

add_action( 'woocommerce_checkout_order_created', 'remove_wc_session_shipping_distance_cost' );function remove_wc_session_shipping_distance_cost( $order ) {    if ( WC()->session->__isset('shipping_distance_cost') ) {        WC()->session->__unset('shipping_distance_cost')    }}

It should work.