Setting Up Taxe Rate Calculations by State/Province

I imagine someone has set this up before, but I cannot find it in the community forums. I have orders all across the country with different tax rates by state/province. I want to be able to set the tax rate for an order based on the state/province from an address field.

What would be the best approach?

Hey @RyanL, with native Knack it wouldn’t be possible to utilise the state field in an address itself, however if you use a State table which has the states and their relevant tax rate, you’ll be able to use the below solution. This is also handy if you want to use a list of connection field values for state selections rather than the user typing it in manually.

Requirements:

  • A table named States with the state and tax rate (e.g. State: CO, Tax Rate: 10%)
  • A table named Orders with an address field (field_xx), and a connection field (field_yy) to the States table
  • A form (view_xx) to add a new Order, containing the address field, and the State connection field (remove the label from this field)

Code:
Add the below code to your app’s JavaScript editor, replacing view_xx, field_xx, and field_yy with your appropriate keys.

$(document).on('knack-view-render.view_xx', function(event, view, record) {

  const addressField = "field_xx"
  const stateField = "field_yy"

  $(`#kn-input-${addressField} input#state`).css("display", "none");
  $(`#${view.key}_${stateField}_chzn`).prependTo(`#kn-input-${addressField} div.control:has(input#state):last`);
  
  $(`#${view.key}-${stateField}`).on("change", function() {
    var selectedOption = $(`#${view.key}-${stateField} option:selected`).text();
    console.log(selectedOption)
    $(`#kn-input-${addressField} input#state`).val(selectedOption);
  });

});

This code will:

  1. Hide the address field’s state field
  2. Move the State connection field dropdown to where the address state was
  3. Listen for any changes to the connection field, and change the value of the hidden address state field to keep both in sync
1 Like