Avatax
For stores that rely on accurate, automated tax calculation across multiple jurisdictions, the official Avalara AvaTax for WooCommerce plugin is the industry standard. This guide explains how to ensure that manually created phone orders also trigger the necessary Avalara tax calculations. Simply installing both plugins is usually sufficient, but a small customisation is available for stores that need to enforce a tax calculation on every single cart update.
The Code: Forcing Avalara to Recalculate Taxes
The Phone Orders for WooCommerce plugin provides an action hook called wpo_before_update_cart. This hook fires just before the cart is updated – for example, when an agent adds a product, changes a quantity, or overrides a price. By attaching a custom function to this hook, you can force the Avalara plugin to recalculate taxes at that precise moment.
The complete code snippet:
|
1 2 3 4 |
//force tax calculations add_action("wpo_before_update_cart", function($cart_data){ add_filter( 'wc_avatax_cart_needs_calculation', "__return_true"); }); |
What This Code Does
| Component | Explanation |
|---|---|
add_action("wpo_before_update_cart", ... ) | Attaches a custom function to the wpo_before_update_cart action hook, which fires every time the phone order cart is about to be updated (e.g., product added, quantity changed, price overridden). |
function($cart_data){ ... } | The custom callback function that receives the current cart data as a parameter. |
add_filter( 'wc_avatax_cart_needs_calculation', "__return_true"); | Inside the callback, this line applies a filter that overrides the Avalara plugin’s internal logic, forcing it to recalculate taxes regardless of whether it thinks a recalculation is necessary. The __return_true function is a WordPress core helper that always returns true. |
When to Use This Code
In most cases, the standard integration between Phone Orders for WooCommerce and the Avalara AvaTax plugin works without any custom code. The standard WooCommerce tax hooks fire when an order is created or updated, and the Avalara plugin responds accordingly.
However, there are specific scenarios where this additional code is beneficial:
- Certain stores have reported that the AvaTax plugin does not automatically calculate taxes on the phone order page, primarily when the order is being created or when the customer or cart details are updated.
- When the AvaTax plugin is not automatically recalculating taxes after every cart change. For example, if an agent overrides a product price, the AvaTax plugin may not register this change as a trigger for recalculation.
- If you find that taxes are not updating consistently and you need to manually click the Recalculate button every time, this code can automate that step.
Important: Do not add this code unless you have confirmed that your store is experiencing a specific issue with tax calculations in the phone order interface. Adding unnecessary filters can sometimes lead to unexpected behaviour or a slight performance overhead.