加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将“零税率”设置为某些自定义预订产品类型

发布时间:2020-12-13 17:58:00 所属栏目:PHP教程 来源:网络整理
导读:有了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_adm
有了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_tours和st_activity产品类型激活“零税”?

谢谢

随着您的问题上次更新,这很容易…您的产品有自定义预订类型.您有3种预订类型,并希望获得除“st_hotel”产品类型之外的所有产品类型的“零税率”.

所以代码将是:

add_filter( 'woocommerce_product_tax_class',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文件中,或者放在任何插件文件中.

此代码已经过测试并且有效…

代码中的错误

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读