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!
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:
- It retrieves the global
$product
object. - It gets the shipping class ID of the product using
$product->get_shipping_class_id()
. - 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.
- It echoes out the shipping class name inside a
- It retrieves the shipping class term object using
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>';
}
}
}
Great again!
Just one question from beginner, there is no div closing tag for the shipping class?
Thanks.
I feel like a beginner myself now LOL! You’re right, I forgot that one – I’ve now fixed it. Thank you
It works great, Thank You!
Awesome!