【问题标题】:Set "Zero Tax" rate to a some custom booking product types将“零税率”税率设置为一些自定义预订产品类型
【发布时间】:2017-06-30 20:31:00
【问题描述】:

对于 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

如何为st_toursst_activity 产品类型激活'Zero Tax'

谢谢

【问题讨论】:

  • @LoicTheAztec 这是一个预订系统,在 woocommerce 的配置中为所有活动征税,主题没有禁用旅游和活动税的功能,当预订旅游时, 主题创建 woocommerce 产品,但地方 税,我需要取消仅旅游和活动的税
  • @LoicTheAztec 我的英文不好,可以发给你主题吗?而你帮助我?非常感谢编辑我的问题!

标签: php wordpress woocommerce product tax


【解决方案1】:

根据您上次更新的问题,这很容易...您的产品具有自定义预订类型。您有 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 文件或任何插件文件中。

此代码已经过测试并且可以工作……


代码错误

【讨论】:

  • 非常感谢!你太棒了....当我在functions.php中添加代码时也出现错误500
  • 我已更正这是一个拼写错误……$product_type = $cart_item['st_booking_data']['st_booking_post_type']; 末尾缺少;……抱歉:)
猜你喜欢
  • 2017-02-11
  • 2017-04-13
  • 2023-03-20
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2021-08-12
  • 2019-05-06
  • 2020-09-18
相关资源
最近更新 更多