Zero-Rate Tax Class for a Category
Applies a different tax class, such as Zero rate or Reduced rate, to every product in chosen categories, so you do not have to edit each product.
Opens WPIDE on
Code
$wpide_tax_class_cats = array( 'books', 'childrens-clothing' ); // Category slugs.
$wpide_tax_class_slug = 'zero-rate'; // Tax class slug.
$wpide_tax_class = function ( $class, $product ) use ( $wpide_tax_class_cats, $wpide_tax_class_slug ) {
$id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
return has_term( $wpide_tax_class_cats, 'product_cat', $id ) ? $wpide_tax_class_slug : $class;
};
add_filter( 'woocommerce_product_get_tax_class', $wpide_tax_class, 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', $wpide_tax_class, 10, 2 );
Notes
Use the tax class slug from WooCommerce > Settings > Tax (zero-rate, reduced-rate); variations follow their parent's categories.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.