WC_Tax::get_tax_class_by()public staticWC 3.7.0

Get an existing tax class.

Method of the class: WC_Tax{}

No Hooks.

Return

Array|true|false. Returns the tax class as an array. False if not found.

Usage

$result = WC_Tax::get_tax_class_by( $field, $item );
$field(string) (required)
Field to get by. Valid values are id, name, or slug.
$item(string|int) (required)
Item to get.

Changelog

Since 3.7.0 Introduced.

WC_Tax::get_tax_class_by() code WC 9.4.2

public static function get_tax_class_by( $field, $item ) {
	if ( ! in_array( $field, array( 'id', 'name', 'slug' ), true ) ) {
		return new WP_Error( 'invalid_field', __( 'Invalid field', 'woocommerce' ) );
	}

	if ( 'id' === $field ) {
		$field = 'tax_rate_class_id';
	}

	$matches = wp_list_filter(
		self::get_tax_rate_classes(),
		array(
			$field => $item,
		)
	);

	if ( ! $matches ) {
		return false;
	}

	$tax_class = current( $matches );

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