如何在税后计算的 woocommerce 中添加自定义费用

How can I add a custom fee in woocommerce that is calculated after tax

我已使用以下代码向我的结帐页面添加自定义费用:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

    $percentage = 0.03;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $woocommerce->cart->tax_total ) * $percentage;
    $woocommerce->cart->add_fee( 'My Fee', $surcharge, false, '' );

}

除了加税外,一切都按预期进行。我想添加

$woocommerce->cart->tax_total

可以解决问题,但该值返回 0。

在计算此费用之前,是否有一些方法可以计算税金?

我不确定这是否是正确处理此问题的方法。

但是,我找到了解决问题的有效方法。

我刚刚计算了税金并将其添加到费用计算中。

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

    $percentage = 0.03;
    $taxes = array_sum($woocommerce->cart->taxes);
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes ) * $percentage;   
    // Make sure that you return false here.  We can't double tax people!
    $woocommerce->cart->add_fee( 'Processing Fee', $surcharge, false, '' );

}

我从 Woocommerce documentation 中得到以下行。注意将 taxable 变量设置为 false,以免双重征税。

$taxes = array_sum($woocommerce->cart->taxes);

获取总税金的方法如下:

$total_taxes = array_sum($woocommerce->cart->get_taxes());