This snippet creates a custom text area field when creating a new shipping method and outputs the data entered as a tooltip on the cart and checkout where the shipping methods are shown. There are a few plugins out there which has this functionality minus the tooltip. Putting the custom shipping method description in a tooltip popup allows for a much cleaner checkout process rather than it being cluttered with lots of text. More than likely, you are going to need to tweak the CSS in this snippet. This CSS has been taken from W3chools Tooltip guide. The style of the shipping methods is typically generated by the theme. This snippet is tailored towards WooCommerce’s Storefront Theme but can easily be tweaked to work with your theme. You’ll see from the CSS part of this snippet, Font Awesome icons are loaded – this is for the circular question mark. You don’t need this line of code if your theme already loads Font Awesome.
/**
* Snippet Name: WooCommerce Shipping Method Description Tooltips
* Snippet Author: ecommercehints.com
*/
// Create a filter for each shipping method
add_action( 'init', 'ecommercehints_init', 100 );
function ecommercehints_init() {
$shipping_methods = WC()->shipping->get_shipping_methods();
foreach ( $shipping_methods as $id => $shipping_method ) {
add_filter( "woocommerce_shipping_instance_form_fields_$id", 'ecommercehints_create_shipping_method_description_field' );
}
}
// Create custom description field in shipping method editor and identify where it will be shown
function ecommercehints_create_shipping_method_description_field( $fields ) {
$new_fields = array(
'description' => array(
'title' => 'Description',
'type' => 'textarea',
),
);
$keys = array_keys( $fields );
$index = array_search( 'title', $keys, true );
$pos = false === $index ? count( $fields ) : $index + 1;
return array_merge( array_slice( $fields, 0, $pos ), $new_fields, array_slice( $fields, $pos ) );
}
// Create the description field as meta
add_filter( 'woocommerce_shipping_method_add_rate_args', 'ecommercehints_create_description_as_meta', 10, 2 );
function ecommercehints_create_description_as_meta( $args, $method ) {
$args['meta_data']['description'] = htmlentities( $method->get_option( 'description' ) );
return $args;
}
// Display the tooltip next to the shipping method title
add_action( 'woocommerce_after_shipping_rate', 'ecommercehints_output_shipping_method_tooltips', 10 );
function ecommercehints_output_shipping_method_tooltips( $method ) {
$meta_data = $method->get_meta_data();
if ( array_key_exists( 'description', $meta_data ) ) {
$description = apply_filters( 'ecommercehints_description_output', html_entity_decode( $meta_data['description'] ), $method );
if ($description) {
$html = ' ' . wp_kses( $description, wp_kses_allowed_html( 'post' ) ) . '';
echo apply_filters( 'ecommercehints_description_output_html', $html, $description, $method );
}
}
}
// Tooltip styling
add_action('wp_head', 'ecommercehints_tooltip_css');
function ecommercehints_tooltip_css() { ?>
WooCommerce Custom Shipping Method Description Field

2 Responses
Hi there, this is flagging as having errors when using it on Advanced Scripts.
the issue seems to happen when I use the https://ecommercehints.com/woocommerce-estimated-delivery-dates snippet alongside this one.
It might be a hook that has already been used. Removing one over the other and everything works ac expected. Any help is much appreaciated.
Thanks
Darren
Hey Darren,
You are spot on. An error is thrown because the same function name is being used in the other snippet.
Each snippet should use a unique function name.
Cheers,
Alex