dongqie2028 2019-03-13 18:00
浏览 44

WooCommerce添加非应税费用,购物车折扣计算错误的税额

I have a WooCommerce site where products may have a non-taxable deposit fee, may have a delivery fee ($7.50), and may have a discount. Without applying the negative fee (discount) the tax is calculated correctly. Once I add the negative fee, the tax includes the non-taxable deposit fee in its calculation. I read somewhere that negative fees are not recommended. I also found this post but don't know if this applies here. Is there another way to accomplish this in the cart and also show in the orders, emails, etc.? FYI the tax rate is 15%. Here's the code I'm using:

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

UPDATE: I found this post Apply a discount on the cart content total excluding taxes in WooCommerce which says that using a negative fee will cause the taxes to always get applied. Is there an alternative method to apply discounts in the cart other than to use negative fees or coupons?

  • 写回答

1条回答 默认 最新

  • dsfovbm931034814 2019-03-13 18:24
    关注
    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;
    }
    

    Tried this way?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!