2

I’m creating an AvaTax Tax Calculator extension for Expresso Store utilizing the ‘store_order_taxes’ hook.

After getting a response from the API, I'm modifying the value of the tax rate as follows:

$order->tax_name = $tax_response_object->TaxLines[0]->TaxDetails[0]->TaxName;
$order->tax_rate = $tax_response_object->TaxLines[0]->Rate;
$order->order_tax = $order->order_total * $order->tax_rate;
$order->order_total = $order->order_total + $order->order_tax;

Is this the correct way to modify these properties? It seems to be working but, to be honest, it seems too easy. It's the first time I've written an Expresso extension (and my first Expresso store) so I'm a bit cautious.

Any help/guidance would be much apprecaited.

3

Yes I don't see any problem with that if it's just for a simple tax adjustment. We don't look to make things deliberately hard. :)

You could also look to set it up as an actual tax adjustment if you wanted to however. Adjustments were introduced in Store v2 to handle extra line items for anything which is not a product (e.g. shipping, discounts, taxes). Basically you write an Adjuster class, which is passed each order, and returns an array of adjustments. Each adjustment will either add or subtract from the order total (this happens automatically, all you need to do is return an array of appropriate adjustments).

Then in your templates we provide the {adjustments}{/adjustments} loop and corresponding tags to easily loop through and display any adjustments you've added/subtracted from the order.

Like I said though for a simple adjustment there's nothing wrong with how you currently have it but it's useful to know about adjustments if you need them in the future.

| improve this answer | |
  • That's exactly what I'm going to do. I'll post the code here when it's done. I've also written an extension for Zip-Tax during my trials which I'll post as well. Thanks for the response and direction, sir! – Jbird Apr 30 '14 at 15:41
  • @jbird please do post your code and extension for Zip-Tax -- I'm looking to do something similar, and seeing your code would be very helpful! – Jay F Apr 30 '14 at 16:57
  • @Ian I have the extension working correctly but I'm getting the following PHP warning: Severity: Warning Message: Invalid argument supplied for foreach() Filename: Adjuster/TaxAdjuster.php Line Number: 38 Based on the the approach I outlined in the original question, do you have any suspicion why this warning is occuring? – Jbird May 1 '14 at 12:54
  • @jbird Did you end up getting your extension to work with AvaTax? We have a similar need and I was wondering if you are able to share that code. Thanks! – caseyreid Apr 2 '15 at 16:53
  • @caseyreid - sorry for the delayed response. I did end up writing the extension but I have since changed jobs and no longer have access to the codebase. Sorry! – Jbird Apr 15 '15 at 18:47
2

To use the store_order_taxes hook correctly, you should append a Store\Model\Tax object to the list of existing taxes. That way, Store will automatically make all the necessary adjustments to the order totals for you (you shouldn't need to edit the order directly).

If you change the $order->order_tax and $order->order_total variables directly, you may end up with some calculations not correctly including your tax (there are lots of different totals stored with the order). So it's best to use the built in Tax class and keep everything in sync.

An example of what this would look like in an extension:

public function store_order_taxes($order, $taxes)
{
    // make API query here

    // create new tax object
    $tax = new \Store\Model\Tax();
    $tax->name = 'My Custom Tax';
    $tax->rate = 0.15;
    $tax->included = false;
    $tax->apply_to_shipping = true;

    // add to list of taxes to apply to this order
    $taxes[] = $tax;

    return $taxes;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.