Get WooCommerce shipping classes

/**
 * Gets WooCommerce shipping classes.
 *
 * @see https://wpcodebook.com/get-woocommerce-shipping-classes/
 *
 * @return array Term ID => Class name
 */
function wpcodebook_get_woocommerce_shipping_classes() {

	$terms = get_terms( array( 'taxonomy' => 'product_shipping_class', 'hide_empty' => false ) );
	$terms = ( $terms && ! empty( $terms ) && ! is_wp_error( $terms ) ? wp_list_pluck( $terms, 'name', 'term_id' ) : array() );

	$terms = array_replace( array( '' => __( 'No shipping class', 'woocommerce' ) ), $terms );

	return $terms;

}

Example output:

Array
(
	[] => No shipping class
	[26] => Class1
	[27] => Class2
)

Leave a Comment