Get Shipping Class ID or Slug

In this tutorial I would like to show you how you can either find both shipping class ID and slug in WordPress admin or get them programmatically using WC_Product object’s methods.

Find a Shipping Class ID and Slug in WooCommerce Settings

Let’s begin with a little bit of WooCommerce settings exploration.

If you’ve ever created a Shipping Class then you will easily find its slug on the same page WooCommerce > Settings > Shipping > Shipping Classes.

But what about a shipping class ID? There are multiple ways how you can find it. First of all – you can inspect the code in your browser and find it there. In order to do that – please open Edit Product page and find “Product data” metabox. Go to “Shipping” tab and inspect “Shipping class” dropdown element.

how to find a shipping class ID when inspecting code in the browser
So, 22 and 24 are shipping classes IDs here.

Add ID column to shipping classes table

But what if you’re uncomfortable with inspecting the code in your browser? In that case I can suggest you to add “ID” column to the shipping classes table in WooCommerce settings.

Here is how it looks like:

how to add a shipping class ID column into WooCommerce settings
I added “ID” column just after “Slug” column.

In order to achieve that I used two filter hooks – woocommerce_shipping_classes_columns and woocommerce_shipping_classes_column_{COLUMN ID}.

/**
 * @snippet       Shipping Class ID Column
 * @author        Misha Rudrastyh
 * @url           https://rudrastyh.com/woocommerce/how-to-get-shipping-class-id.html
 */
add_filter( 'woocommerce_shipping_classes_columns', 'rudr_add_shipping_class_column' );

function rudr_add_shipping_class_column( $shipping_class_columns ) {

	$shipping_class_columns = array_slice( $shipping_class_columns, 0, 2 ) + array( 'id' => 'ID' ) + array_slice( $shipping_class_columns, 2, 3 );
	return $shipping_class_columns;

}

// woocommerce_shipping_classes_column_{COLUMN ID}
add_action( 'woocommerce_shipping_classes_column_id', 'rudr_populate_shipping_class_column' );

function rudr_populate_shipping_class_column() {

	echo '{{ data.term_id }}';

}

You can insert this code to your current or child theme functions.php file.

Get Shipping Class ID or Slug Programmatically

All you need to do here is to use either get_shipping_class_id() or get_shipping_class() method of WC_Product object.

As easy as this:

$product = wc_get_product( $product_id );

// print shipping class ID
echo $product->get_shipping_class_id(); // 22
// print shipping class slug
echo $product->get_shipping_class(); // misha-custom

Get a shipping class ID by its slug

Let’s assume here that you don’t have WC_Product object available, all you have is just a specific shipping class slug, “misha-custom” for example. How to get its ID?

Did you know that shipping classes is just a custom taxonomy created by WooCommerce? Because that knowledge gives us the superpower to use get_term_by() function!

$shipping_class = get_term_by( 'slug', 'misha-custom', 'product_shipping_class' );

echo $shipping_class->term_id; // 22
echo $shipping_class->name; // Misha custom (shipping class name)

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X