WordPress at Your Fingertips
function is not described

WC_Tax::create_tax_class() public WC 3.7.0

Create a new tax class.

{} It's a method of the class: WC_Tax{}

No Hooks.

Return

WP_Error|Array. Returns name and slug (array) if the tax class is created, or WP_Error if something went wrong.

Usage

$result = WC_Tax::create_tax_class( $name, $slug );
$name(string) (required)
Name of the tax class to add.
$slug(string)
(optional) Slug of the tax class to add.
Default: sanitized name

Changelog

Since 3.7.0 Introduced.

Code of WC_Tax::create_tax_class() WC 5.9.0

public static function create_tax_class( $name, $slug = '' ) {
	global $wpdb;

	if ( empty( $name ) ) {
		return new WP_Error( 'tax_class_invalid_name', __( 'Tax class requires a valid name', 'woocommerce' ) );
	}

	$existing       = self::get_tax_classes();
	$existing_slugs = self::get_tax_class_slugs();
	$name           = wc_clean( $name );

	if ( in_array( $name, $existing, true ) ) {
		return new WP_Error( 'tax_class_exists', __( 'Tax class already exists', 'woocommerce' ) );
	}

	if ( ! $slug ) {
		$slug = sanitize_title( $name );
	}

	// Stop if there's no slug.
	if ( ! $slug ) {
		return new WP_Error( 'tax_class_slug_invalid', __( 'Tax class slug is invalid', 'woocommerce' ) );
	}

	if ( in_array( $slug, $existing_slugs, true ) ) {
		return new WP_Error( 'tax_class_slug_exists', __( 'Tax class slug already exists', 'woocommerce' ) );
	}

	$insert = $wpdb->insert(
		$wpdb->wc_tax_rate_classes,
		array(
			'name' => $name,
			'slug' => $slug,
		)
	);

	if ( is_wp_error( $insert ) ) {
		return new WP_Error( 'tax_class_insert_error', $insert->get_error_message() );
	}

	wp_cache_delete( 'tax-rate-classes', 'taxes' );

	return array(
		'name' => $name,
		'slug' => $slug,
	);
}