Get chosen shipping method title by its Id in WooCommerce

developersaumya picture developersaumya · Jun 8, 2017 · Viewed 12.8k times · Source

In WooCommerce includes\class-express-checkout-gateway.php file I am getting shipping method Id by the following code:

$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');

It gets flat_rate:1, but I need its title - Flat Rate.

I have already tried all this answers and many more.

How can I get this?

Answer

mujuonly picture mujuonly · Jun 8, 2017
$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