Set "Zero Tax" rate to a some custom booking product types Set "Zero Tax" rate to a some custom booking product types wordpress wordpress

Set "Zero Tax" rate to a some custom booking product types


With your question last update, this is very easy... Your products have a custom booking type. You have 3 kinds of booking types and you want to get the "Zero Tax" rate for all product types except 'st_hotel' product type.

So the code is going to be:

add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );function wc_diff_rate_for_user( $tax_class, $product ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    // Iterating through each cart items    foreach(WC()->cart->get_cart() as $cart_item){        if($product->id == $cart_item['product_id']){            $booking_post_type = $cart_item['st_booking_data']['st_booking_post_type'];            break;        }        $count++;        echo 'Product ' . $count . ' has a post type of: ' .$cart_item['st_booking_data']['st_booking_post_type']. '<br>;    }    // If the booking post type is different than 'st_hotel', the "Zero Tax" rate is applied    if( 'st_hotel' != $booking_post_type ){        $tax_class = 'Zero Rate';    }    return $tax_class;}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works…


error in the code