Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

With WooCommerce, I am using Traveler premium theme

I need to deactivate (TAX) in the tours and hotels, And I am trying to use this code for it:

add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );function wc_diff_rate_for_user( $tax_class, $cart_item ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

// Define HERE your targeted products IDs
$products_ids_arr = array(12 ,15, 24);

// Define HERE your targeted user roles
$users_role_arr = array('administrator', 'userrolename');

//Getting the current user data
$user_data = get_userdata(get_current_user_id());

foreach ($users_role_arr as $user_role)
    if ( in_array( $user_role, $user_data->roles ) && in_array( $cart_item->id, $products_ids_arr ) ) {
        $tax_class = 'Zero Rate';
        break;
    }

return $tax_class;}

This code come from this answer: Tax class "Zero rate" per user role on specific product ID's

But there is no way and this is not the option. I can't make it work to inactivate Tax only for tours and hotels.

I need some help, to understand how I can achieve disabling taxes only for tours and hotels within my theme in WooCommerce.


IMPORTANT UPDATE

I am using this little peace code to output my custom product types from cart, where I have the 3 different kinds of products types:

add_action('woocommerce_before_cart_table','output_cart_raw_data');
function output_cart_raw_data(){
    $count = 0;
foreach(WC()->cart->get_cart() as $cart_item){
    $count++;
    echo 'Product ' . $count . ' has a post type of: ' . $cart_item['st_booking_data']['st_booking_post_type'] . '<br>;
}
}

It outputs in my cart page this (the product types used):

Product 1 has a post type of: st_tours
Product 2 has a post type of: st_hotel
Product 3 has a post type of: st_activity

How can I get 'Zero Tax' activated for st_tours and st_activity product types?

Thanks

like image 273
Juan David Avatar asked Feb 12 '17 22:02

Juan David


1 Answers

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

like image 174
LoicTheAztec Avatar answered Oct 10 '22 08:10

LoicTheAztec