WooCommerce: Display Shipping Class @ Single Product Page

In certain cases, you may want to show the name of the current product’s shipping class. This is helpful especially for B2B stores, or when the shipping class name is very descriptive and helps the customer with their shopping choices.

Of all the places where we can print the shipping class, I chose the “Product Meta” section, which already shows the product SKU, Tags and Categories – this code will add another line called “Shipping class”, together with the name of the class (if any, of course). Enjoy!

Here’s a brand new line under the product “meta”, where you can print the product shipping class name.

PHP Snippet: Display Shipping Class Name @ Single Product Page

The first line of the code below adds an action to the ‘woocommerce_product_meta_end‘ hook, which is triggered at the end of the product meta section in WooCommerce. It specifies that the function bbloomer_echo_shipping_class should be called when this hook is triggered.

The function bbloomer_echo_shipping_class retrieves the shipping class information for the current product and echoes it out. Here’s what it does:

  1. It retrieves the global $product object.
  2. It gets the shipping class ID of the product using $product->get_shipping_class_id().
  3. If a shipping class ID exists:
    • It retrieves the shipping class term object using get_term_by() by passing the ID, taxonomy ‘product_shipping_class’, and the ID itself.
    • If the term object exists and is not a WordPress error:
      • It echoes out the shipping class name inside a <div> element.

Overall, this code adds the shipping class information to the product meta section on the single product page in WooCommerce.

/**
 * @snippet       Shipping Class Name @ WooCommerce Single Product
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_action( 'woocommerce_product_meta_end', 'bbloomer_echo_shipping_class' );

function bbloomer_echo_shipping_class() {
	global $product;
	$class_id = $product->get_shipping_class_id();
	if ( $class_id ) {
		$term = get_term_by( 'id', $class_id, 'product_shipping_class' );
		if ( $term && ! is_wp_error( $term ) ) {
			echo '<div>' . __( 'Shipping class', 'woocommerce' ) . ': ' . $term->name . '</div>';
		}
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Display Shipping Class @ Single Product Page

  1. Great again!
    Just one question from beginner, there is no div closing tag for the shipping class?
    Thanks.

    1. I feel like a beginner myself now LOL! You’re right, I forgot that one – I’ve now fixed it. Thank you

      1. It works great, Thank You!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *