重新计算 WooCommerce 上已完成的订单税,以从订单总额中减去税费

问题描述 投票:0回答:1

这是我面临的情况,我感谢我能得到的任何帮助:

情况是这样的,我有一个与 WooCommerce 集成的 WordPress 商店。

设置:

产品价格含税。 WooCommerce 插件德语化

情况:WooCommerce 税收功能已禁用一段时间,但仍有订单下达,因此未计算税收。现在订单已标记为已完成,我想编辑订单并重新计算包括税费的订单。

我已经能够通过在functions.php中添加自定义过滤器来编辑订单

    add_filter( 'wc_order_is_editable', 'make_order_editable', 9999, 2 );
     
    function make_order_editable( $allow_edit, $order ) {
        if ( $order->get_status() === 'completed' ) {
            $allow_edit = true;
        }
        return $allow_edit;
    }

否则将订单状态更改为待付款,以便我可以重新计算订单。

嗯,当重新计算订单时,税费将添加到订单总额中,这是 woocommerce 的默认行为。

要解决的问题:我希望重新计算以从订单总额中减去税费。

我在functions.php上尝试了多个自定义代码,但没有任何效果,我将在下面列出它们:

function custom_recalculate_order_total($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
    if (isset($_POST['calculate-action'])) {
        // Get the order ID
        $order_id = isset($_POST['post_ID']) ? intval($_POST['post_ID']) : 0;

        if ($order_id > 0) {
            // Get the order
            $order = wc_get_order($order_id);
            $order_total = $order->get_total();
            $tax = $order->get_total_tax();
            $new_total = $order_total - $tax;
            $order->set_total($new_total);
            $order->save();
        }
    }
}
add_action('woocommerce_after_save_order_items', 'custom_recalculate_order_total', 10, 1);
function custom_adjust_tax_on_recalculate($taxes, $cart) {
    if (isset($_POST['calculate-action'])) {
        // Get the order subtotal before tax
        $subtotal = $cart->cart_contents_total;
        $tax_rate = 0.10;
        $tax_amount = $subtotal * $tax_rate;
        $taxes = array(
            'tax_total' => $tax_amount,
        );
    }
    return $taxes;
}
add_filter('woocommerce_cart_totals_taxes', 'custom_adjust_tax_on_recalculate', 10, 2);
add_action("woocommerce_order_before_calculate_taxes", "custom_order_before_calculate_taxes", 10, 2);
function custom_order_before_calculate_taxes($order, $args) {
    if (isset($_POST['calculate-action'])) {
        $subtotal = $order->get_subtotal();
        $tax_rate = 0.10;
        $tax_amount = $subtotal * $tax_rate;
        $order->set_total_tax($tax_amount);
    }
}

请随时索取您可能需要的任何额外信息。

我检查过的一些相关问题:WooCommerce:向旧(已完成)订单添加税 Woocommerce 重新计算已完成订单的税会忽略产品中包含的增值税 WooCommerce 添加用于重新计算按钮的订单管理挂钩

wordpress woocommerce hook-woocommerce
1个回答
0
投票

你是如何解决这个问题的? 我有一个非常相似的问题。 我可以重新计算数据库中的价格,但运费和税费有问题。

© www.soinside.com 2019 - 2024. All rights reserved.