Possible to add tax values based on API response?
-
Hey, WooComm dev community –
James here. Relative newcomer to WooCommerce, but enjoying the experience and challenge so far. I am currently working on a WooComm project for a client who wants me to fetch tax information by submitting the cart contents and user locale data to an API (the “Tax API”), which sends back hardcoded tax values – not rates, actual values based on the client’s internal calculations. The code to query the API and to receive the response is written – I just need to know where to stick it. Is there an appropriate action hook for this scenario? Some form of the
calc_tax()
function I could inject the API query logic into? So far, everything I’m seeing about adding taxed programmatically (like this function) has to do with adding tax rates, not actual end-of-the-line tax values.They wanted the same kind of setup re: shipping – that is, submitting the cart contents to an API to determine shipping costs. I succeeded here by creating a custom shipping method (as a plugin) and injecting the API query logic into the
calculate_shipping()
function of that shipping method. This is working great – is there something similar I could do around taxes?I thought of another way to achieve my goal, but it’s pretty hack-y: I could disregard and disable the whole WooComm tax system entirely, and add the Tax API response total as a “fee” instead (while displaying it on the front end as the order tax, of course). Would this work? Would it cause problems I’m not foreseeing?
I like this whole API-based approach because, A.) the client really wants it, and B.) they wholly manage this tax rate data and I don’t have to think about it – if the rates are off, it’s their problem. But I am discovering that tacking on hardcoded tax values doesn’t appear to be the standard way of calculating taxes in WooComm. If this method isn’t possible, I think I can get them to edit the API to send rates rather than values. But I wanted to seek some second opinions first and try to bend their way. Thanks in advance to anyone who’s read this far!
- The topic ‘Possible to add tax values based on API response?’ is closed to new replies.