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
 * @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

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

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

2 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

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 *