• 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!

    https://wordpress.org/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Hi James

    What API is this can I ask? I know some developers have successfully done the same thing with Tax Jar, Tax Cloud (I think its called) and Avalara.

    Thread Starter jameskanestl

    (@jameskanestl)

    Hey, Mike. Thanks for taking an interest. It’s actually just a custom-baked REST API created by the client that integrates with their CRM and local databases. I send a query that looks like this:

    {
        "customerno":"ABC0001001",
        "state":"Georgia",
        "zip":"56544",
        "zipplus":"0000",
        "volume":"1,100.0",  // cart subtotal
        "freight":"10.5"  // shipping costs, also calculated via API
    }

    And it returns JSON like this:

    {
        "state": 76.346875000000011,
        "county": 5.5525,
        "city": 0,
        "district": 0
    }

    Those tax values are based on the client’s internal tax tables, customer non-profit status and some discount programs.

    Again, I’ve considered just disabling WooComm tax entirely, and tacking these values on to the cart as “Fees.” But if there is a good tax action hook or programatic way to directly inject tax values, that would be preferable, I suppose. As a last ditch, I think I could get them to edit the API to return the rates rather than the values, but even then I’m not 100% sure what the ideal approach would be… Any thoughts? Resources to help me look into the issue of tax calculation further? I know there’s a way to make this work.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Grab the source of https://www.woothemes.com/products/woocommerce-taxamo/ and look how that does it. I think its a similar system.

    Thread Starter jameskanestl

    (@jameskanestl)

    Took a look at Taxamo. Could be wrong, but it seemed to me like they are still relying on rates, even if they are getting those rates via an API call. Thanks for the tip, nevertheless.

    Just to test the idea, I disabled all taxes and tax calculations from the Admin panel. I then plugged my API query logic into a new function in functions.php:

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_taxes_as_fee' );
    function woocommerce_custom_taxes_as_fee() {
    
    		// JSON for API call formulated and sent
    		// API response JSON received and tabulated as $total_tax
    
    		$woocommerce->cart->add_fee( 'Tax', $total_tax, true, '' );
    
    		// Error checking code, too, of course
    	}
    }

    Basically, because I named the fee “Tax” when I call add_fee(), it displays in the cart just below the subtotal exactly as if it were the standard tax calculation. The values I want get plugged in, the customer is charged the correct amount, and the amounts are tracked as necessary…

    Not exactly semantically on point but it seems like a good solution. Can you think of any potential downsides? As long as it’s well-documented and maintained, I hope this is a decent solution for this use case.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    The disadvantage of using fees is you’re going to lose a link between line items and taxes. This will mean more manual calculation if you need to do refunds, and you’ll have less working reporting.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Possible to add tax values based on API response?’ is closed to new replies.