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

时间:2015-10-02 20:22:32

标签: php wordpress woocommerce e-commerce

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

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。

在计算此费用之前,有没有办法计算税?

2 个答案:

答案 0 :(得分:1)

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

但是,我已经找到了解决问题的方法。

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

<select ng-model="cusPro.feet" ng-options="feetLength for feetLength in feetLengths">
<select ng-model="cusPro.inches" ng-options="inchesLength for inchesLengthin inchesLengths">

You selected: {{cusPro.feet}} feet, {{cusPro.inches}} inches

我从Woocommerce documentation获得了以下一行。注意将应税变量设置为假,以免使税人加倍。

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

}

答案 1 :(得分:0)

以下是如何获取总税金:

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