2017-02-12 46 views
7

隨着WooCommerce,我使用Traveler溢價主題集「零稅」率的一些定製預訂產品類型

我需要停用(TAX)在旅遊和酒店,我嘗試使用此代碼它:

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;} 

來此代碼從這樣的回答:Tax class "Zero rate" per user role on specific product ID's

但也沒辦法,這是不是選項。我不能讓它工作,只爲旅遊和酒店滅稅。

我需要一些幫助,瞭解如何才能在WooCommerce中爲我的主題中的旅遊和酒店實現禁用稅。


重要更新

我用這一點和平的代碼輸出從車我自定義的產品類型,在那裏我有3種不同的產品類型:

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>; 
} 
} 

它在我的購物車頁上的輸出(使用的產品類型):

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 

我怎樣才能得到'Zero Tax'激活的st_toursst_activity產品類型?

感謝

+0

模板這個https://themeforest.net/item/traveler-traveltourbooking-wordpress-theme/10822683 –

+0

@LoicTheAztec它是一種預訂系統,在woocommerce的配置中主動爲所有物品徵稅,主題不具有對遊覽和活動禁用稅收的功能,當預訂到遊覽時,主題創建woocommerce產品,但將地點稅,我需要刪除只有旅遊和活動稅 –

+0

@LoicTheAztec我的英語是好,我可以給你發送主題嗎?你幫助我嗎?非常感謝編輯我的問題! –

回答

2

你的問題最後一次更新,這是很容易的......你的產品有一個自定義的預訂類型。您有3種預訂類型,您希望獲得除「st_hotel」產品類型以外的所有產品類型的「零稅率」。

因此,代碼將是:

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; 

} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

該代碼測試和工程......在代碼


錯誤

+0

非常感謝!你很棒....也錯誤500當我在functions.php中添加代碼 –

+0

我糾正了它是一個錯字錯誤...缺少';'在'$ product_type = $ cart_item ['st_booking_data'] [ 'st_booking_post_type'];'......抱歉:) – LoicTheAztec

相關問題