How to Find Shipping Class ID?

When I create conditions with shipping classes in my code I usually use get_shipping_class() method, so I can check against shipping classes slugs, example. Slugs can easily be found in WooCommerce > Settings > Shipping > Shipping Classes.

But what you have to use get_shipping_class_id() method? Where and how to get a specific shipping class ID then?

First of all – you can inspect the code in your browser and find it there. In order to do that – please open product edit page and find “Product data” metabox. Go to “Shipping” tab and inspect “Shipping class” dropdown element.

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.

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 Twitter