WC_Tax::get_tax_class_by
Get an existing tax class.
Method of the class: WC_Tax{}
No Hooks.
Returns
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() WC Tax::get tax class by code WC 10.3.6
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,
);
}