Get current shipping method in WooCommerce

/**
 * Gets current shipping method ids.
 *
 * @see https://wpcodebook.com/woocommerce-get-current-shipping-method/
 */
function wpcodebook_get_chosen_shipping_method_ids() {

	if ( ! function_exists( 'WC' ) ) {
		return false;
	}

	if ( empty( WC()->session ) ) {
		return array();
	}

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

	if ( empty( $shipping_methods ) ) {
		return array();
	}

	return array_map( function ( $shipping_method ) {
		$shipping_method = explode( ':', $shipping_method );
		return $shipping_method[0];
	}, $shipping_methods );

}

Example output:

Array
(
    [0] => free_shipping
)

Leave a Comment