• Resolved djerrard

    (@djerrard)


    (I reviewed the first four pages of results from searching this forum for ‘custom tax calculator’, but found no answers.)

    I wrote a custom tax calculation plugin that works 100% correctly if I use it with the ‘woocommerce_cart_calculate_fees’ action. That is, it calculates correct values and it shows as a fee on the cart page.

    Why the custom plugin?

    My plugin uses data from an ERP system (and not the WooCommerce product/tax configuration) to calculate taxes. There are two reasons for this:

      It ensures that when the order information moves from WooCommerce to the ERP, that the taxes line up correctly with the ERP system’s own calculations.
      By using the ERP system as the authority and source for the tax settings of customers and products, there’s no need to also update this data in WooCommerce whenever it is updated in the ERP system. This is a significant benefit to our clients.

    The Problem
    I don’t want this to be a fee. I want this plugin to be called as a replacement/override of WooCommerce’s own tax calculator. Where/how can I do this?

    My plugin needs the WC_Cart instance. The ‘woocommerce_cart_calculate_fees’ action passes a WC_Cart, so my plugin works fine with that.

    However, the ‘woocommerce_calc_tax’ filter, doesn’t pass a WC_Cart. Instead, it looks like it would be called once per line item since it passes an item price (but no item number, nor indication of which line item it is) along with a few other parameters, none of which are sufficient for my plugin to work. The absence of the item number means my plugin can’t consult the ERP system for product-specific tax information.

    In short, I need something like this, where the returned value is what WooCommerce will use as the tax amount:

    
    add_action('???????', 'myplugin_calculate_taxes_from_cart', 10, 1);
    function myplugin_calculate_taxes_from_cart(WC_Cart $cart) : float
    {
      $calculated_tax = my_custom_tax_calculator($cart);
      return $calculated_tax;
    }
    

    Is there a hook that is more suitable?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to Wire Up Custom Tax Plugin’ is closed to new replies.