WooCommerce添加免税的费用和购物车的折扣来计算错误的税额

时间:2019-03-13 18:00:28

标签: php wordpress woocommerce

我有一个WooCommerce网站,在该网站上,产品可能需要支付免税的押金,也可能需要交付运费(7.50美元),并且可能会有折扣。在不应用费用(折扣)的情况下,税款可以正确计算。一旦我加上了负费用,税金便在计算中包括了免税的押金。我在某处读到不建议使用负费用。我也found this post,但不知道这在这里是否适用。有没有其他方法可以在购物车中完成此操作,也可以在订单,电子邮件等中显示?仅供参考,税率为15%。这是我正在使用的代码:

function woocommerce_custom_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        $item_data = $cart_item['data'];
        $deposit = $item_data->get_attribute('deposit');
        $delivery = $cart_item['delivery'];

        if ( $deposit ) {
            $total_deposit += $cart_item['quantity'] * $deposit;
        }

        if ( $delivery == 'deliver' ) {
            $total_delivery += $cart_item['quantity'] * 7.5;
        }
    }

    if ( $total_deposit > 0 ) {
        // non-taxable
        $cart->add_fee( 'Deposit', $total_deposit, FALSE );
    }

    if ( $total_delivery > 0 ) {
        // taxable
        $cart->add_fee( 'Delivery', $total_delivery, TRUE );
    }

    //  test $10 discount
    $cart->add_fee( 'Test discount', -10.00);
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fees', 25, 1 );

correct tax amount without negative fee

incorrect tax amount with negative fee

更新:我发现了这篇帖子Apply a discount on the cart content total excluding taxes in WooCommerce,该帖子说使用负费用会导致始终征税。除了使用负费用或优惠券之外,还有其他方法可以在购物车中应用折扣吗?

1 个答案:

答案 0 :(得分:0)

function woocommerce_custom_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;


        // non-taxable
        $cart->add_fee( 'Deposit', 6, FALSE );



        // taxable
        $cart->add_fee( 'Delivery', 7, TRUE );


    //  test $10 discount
    //$cart->add_fee( 'Test discount', -10.00 , FALSE);
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fees', 25, 1 );



add_filter( 'woocommerce_calculated_total', 'discounted_calculated_total', 10, 2 );
function discounted_calculated_total( $total, $cart ){

    $total = $total - 10;

    return $total;
}

这样尝试过吗?