0

here is a situation I'm facing, I appreciate any help I can get on this:

So here is the situation, I have a WordPress store integrated with WooCommerce.

Setup:

  • product price include tax.
  • Germanized for WooCommerce plugin

Situation:

WooCommerce Tax feature was disabled for a while, but there were orders that were made, as result the tax wasn't calculated. Now that orders are marked as completed, I would like to edit orders and recalculate orders including Tax.

I've been able to edit orders either by adding a Filter:

    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;
    }

Otherwise change order status to pending payment so I can recalculate the order.

Well, when recalculate the orders, Tax is being added to the total of order, this is the default behavior of woocommerce.

PROBLEM TO FIX: I would like the recalculation to subtract Tax from the order total amount.

I've tried multiple custom codes on the functions.php nothing worked I will list them bellow:

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);
    }
}

Feel free to request any extra information you might need.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.