skip to Main Content

I’m working on a challenge where I have to update the tax rate for a specific line item in an existing WooCommerce order based on its SKU. I realize that the method I’m using may not be the best practice, but due to some constraints in my workflow, this is the approach I’ve had to take. Specifically, I aim to change the tax rate from a standard rate to a reduced rate, while retaining the original tax amount.

I’ve succeeded in eliminating the standard tax rate and replacing it with the reduced one. However, the problem arises when trying to retain the original tax amount within the line item after these changes. Clicking "Recalculate" in the WooCommerce dashboard resets the tax back to the standard rate.

Has anyone faced a similar issue or have suggestions to fix this while working within my constraints?

Thank you!

Here is what I’ve been trying so far:

add_action('init', 'update_order_tax_rate');

function update_order_tax_rate() {
    if (isset($_GET['run_my_script']) && $_GET['run_my_script'] == 'true') {
        $order_id = 79556;
        $order = wc_get_order($order_id);

        if (!$order) {
            return;
        }

        // Eliminar la tasa de impuestos anterior
        $taxes = $order->get_taxes();
        foreach ($taxes as $tax) {
            if ($tax->get_rate_code() == 'ES-IVA-1') {
                $order->remove_item($tax->get_id());
            }
        }

        // Añadir nueva tasa de impuestos
        $item = new WC_Order_Item_Tax();
        $item->set_rate(9); // El ID de la tasa de impuesto que quieres aplicar
        $item->set_order_id($order_id);
        $item->save();
        $order->add_item($item);

        // Actualizar el impuesto del ítem con el SKU especificado
        foreach ($order->get_items() as $item_id => $item) {
            $sku = $item->get_meta('SKU'); // Suponiendo que 'SKU' es el metakey
            if ($sku === 'HIB-1000') {
                $subtotal = $item->get_subtotal();
                $new_tax_amount = $subtotal * 0.1;
                $item->update_meta_data('_line_tax', wc_format_decimal($new_tax_amount));
                $item->save();
            }
        }

        $order->save();
    }
}

I’ve tried a variety of approaches to solve this issue. First, I attempted to directly change the tax rate ID on the specific line item, expecting the change to take effect immediately. However, the rate did not change.

Then, I tried removing the existing tax rate and adding a new one with the desired reduced rate. My expectation was that the new tax rate would apply correctly to the specific line item, but this didn’t happen either.

Furthermore, when I click on "Recalculate" in the WooCommerce dashboard, the tax rate reverts back to the standard rate, which is not the desired outcome.

So, essentially, while I can get close to a solution, I haven’t yet been able to make the change stick, specifically for the line item in question.

2

Answers


  1. It seems you’re trying to update the tax rate for a specific item in a WooCommerce order. Although you’ve made some progress, the tax rate keeps reverting to the standard rate after clicking "Recalculate" in the WooCommerce dashboard.

    The challenge here is that WooCommerce recalculates taxes based on its rules and settings, which can override your manual changes. To address this issue, you may need to explore ways to modify WooCommerce’s tax calculation behavior or find a workaround within the constraints of your workflow.

    Unfortunately, there isn’t a straightforward solution within your provided code snippet. You might consider reaching out to WooCommerce experts or forums for specific insights into customizing tax calculations while retaining your changes.

    Login or Signup to reply.
  2. I believe you can achieve this without the need of the code.

    If you go to WooCommerce --> Settings --> Tax you will find a field called Additional Tax Classes. Here you can type in a new tax class name, and save the changes.

    enter image description here

    After that, you will find a new tab in the tax settings, with the name you just entered.

    Tax class tabs

    If you click on that tab, you will be able to set a custom Tax rate with some conditions regarding the country.

    enter image description here

    Now, you can edit the product you wish to apply this tax for. Go to the General tab --> Tax class --> Select the tax class --> save. You can apply this tax class to any product where needed.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search