0

在 woocommerce 中,我使用以下代码(来自此答案线程根据产品类别添加累进费用,并且效果很好:

add_action( 'woocommerce_cart_calculate_fees', 'wc_custom_surcharge', 20, 1 );
function wc_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    ## Your Settings (below) ##

    $categories      = array(19);
    $targeted_states = array('FL');
    $base_rate       = 1;

    $user_state = WC()->customer->get_shipping_state();
    $user_state = empty($user_state) ? WC()->customer->get_billing_state() : $user_state;
    $surcharge  = 0; // Initializing

    // If user is not from florida we exit
    if ( ! in_array( $user_state, $targeted_states ) )
        return; // Exit

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] )  ){
            // calculating fee based on the defined rate and on item quatinty
            $surcharge += $cart_item['quantity'] * $base_rate;
        }
    }

    // Applying the surcharge
    if ( $surcharge > 0 ) {
        $cart->add_fee( __("State Tire Fee", "woocommerce" ), $surcharge, FALSE, '' );
}

我的问题是我想将此费用设置为不征税

我已经尝试了几个代码和东西,但就是不明白。

任何帮助或跟踪将不胜感激。

4

1 回答 1

0

您可以在方法中包含的可用参数上以2种方式处理此问题:WC_Cartadd_fee()

1)不征税:将费用设置为“不征税” (将第三个参数设置为false)

$cart->add_fee( __("State Tire Fee", "woocommerce" ), $surcharge, false );

2)“零税率”应税:使用“零税率”税级将费用设置为“应税” 。

在这种情况下,您需要在 Woocommerce Tx 设置中设置(如果尚未完成)零税率:

在此处输入图像描述

在此处输入图像描述

然后,您将能够在WC_Cart方法中设置add_fee()应税费用,并将第四个参数设置为“零税率”,即税级,这样:

$cart->add_fee( __("State Tire Fee", "woocommerce" ), $surcharge, true, 'zero-rate' );

那些应该工作。

于 2018-12-06T01:24:56.463 回答