首页 文章

在woocommerce中添加自定义购物车负税费问题

提问于
浏览
1

我在我的woocommerce网站上有运费折扣,我想在购物车页面和结帐页面上显示,因为我使用 add_fee() 方法 .

WC()->cart->add_fee( 'Shipping Discount', -20, false );

它从总金额中减去20,但是当我在管理员的订单中检查订单时,它根据我配置的税率给出了税收折扣 . 有没有替代添加折扣

我怎么能在woocommerce中做到这一点?

1 回答

  • 1

    对于负面购物车费用,有一个与税收相关的错误,因为它们适用于所有税收类别,即使它不是应纳税的 .

    现在,您可以制作自定义全球购物车折扣 . 这样您就可以定义所需的折扣类型,折扣金额和要打折的来源价格 .

    此代码将显示正确的购物车折扣总量

    一旦提交,这个折扣的gran total将被传递给订单 .

    代码:

    // Apply a discount globally on cart
    add_filter( 'woocommerce_calculated_total', 'discounted_calculated_total', 10, 2 );
    function discounted_calculated_total( $total, $cart ){
        $amount_to_discount = $cart->cart_contents_total;
        $percentage = 10; // 10% of discount
        $discounted_amount = $amount_to_discount * $discount / 100;
    
        $new_total = $total - $discounted_amount;
        return round( $new_total, $cart->dp );
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 .

    经过测试和工作 .

    您应该制作一些额外的代码来显示折扣金额(在购物车和结帐页面以及订单视图中)并按顺序将其作为元数据传递等等......

相关问题