【问题标题】:Set a Zero tax rate on a cart fee in WooCommerce在 WooCommerce 中为购物车费用设置零税率
【发布时间】:2019-05-06 10:28:51
【问题描述】:

在 woocommerce 中,我使用以下代码根据产品类别添加累进费用(来自此answer thread,效果很好:

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, '' );
}

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce tax fee


    【解决方案1】:

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

    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' );
    

    其中一个应该可以工作。

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多