首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将“零税率”设置为某些定制预订产品类型

将“零税率”设置为某些定制预订产品类型
EN

Stack Overflow用户
提问于 2017-02-12 14:44:54
回答 1查看 443关注 0票数 7

对于WooCommerce,我使用的是旅行者高级主题

我需要在旅游和酒店中停用(税收),我正在尝试使用这个代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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;}

这段代码来自于以下答案:https://stackoverflow.com/questions/40871587/tax-class-zero-rate-per-user-role-on-specific-product-ids/40893064#40893064

但这是不可能的,这也不是选择。我不能只为旅游和旅馆取消税收。

我需要一些帮助,以了解我如何才能实现仅对旅游和酒店在我的主题在WooCommerce的税收。

重要更新

我使用这个小的和平代码从购物车输出我的自定义产品类型,其中我有3种不同的产品类型:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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>;
}
}

它在我的购物车页面输出如下(所使用的产品类型):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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_activity产品类型激活st_tours

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-12 19:17:43

上一次问你的问题,这很容易.您的产品有定制订票类型。您有三种预订类型,您希望得到“零税率”的所有产品类型,但'st_hotel‘产品类型。

所以代码是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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文件中,或者在任何插件文件中。

对此代码进行测试并运行…。

代码中的错误

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42194167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文