Get chosen shipping method title by its Id in WooCommerce
$rate_table = array();$shipping_methods = WC()->shipping->get_shipping_methods();foreach($shipping_methods as $shipping_method){ $shipping_method->init(); foreach($shipping_method->rates as $key=>$val) $rate_table[$key] = $val->label;}echo $rate_table[WC()->session->get( 'chosen_shipping_methods' )[0]];
Try like this
If the customer has calculated the shipping on the front end, this should work:
function get_shipping_name_by_id( $shipping_id ) { $packages = WC()->shipping->get_packages(); foreach ( $packages as $i => $package ) { if ( isset( $package['rates'] ) && isset( $package['rates'][ $shipping_id ] ) ) { $rate = $package['rates'][ $shipping_id ]; /* @var $rate WC_Shipping_Rate */ return $rate->get_label(); } } return '';}
This will return an empty string if it couldn't find a name for the given shipping id.