Creating taxes

To create a tax, you must perform the following:

  1. Get the orders manager.
    Get an instance of the OrdersManager object.
  2. Create a new tax.
    To create a new tax class, call the CreateTax method of the manager.
  3. Set the properties of the Tax object.
    In this example the following properties are set:
    • Title
      Gets or sets the display name of the department.
    • TaxRate
      Gets or sets the default rate for the tax.
    • Country
      Gets or sets the country that the tax is associated with.Note that if the value of this property is "United States", you have to set the State property as well.

      TIP: You can additionally narrow the location for the Tax by using the optional City, County, FromZip, and ToZip properties. Regarding the ZIP related properties, you can use either only the FromZip property for a single ZIP number (default behavior), or to use both of them for a range of ZIP numbers. To do the latter, you must set the UseZipRange property to true. Note that all of these properties should be used only when the Country property is set to "United States".

    • AddressType
      Gets or sets the address type the tax is applied to. You can choose between AddressType.Shipping and AddresType.Billing.
    • ApplyTaxToShipping
      Indicates whether the tax is applied to the shipping.
  4. Specify rate values for each available tax class.
    To do this, perform the following:
    1. Create a List of TaxClassRate objects.
    2. Create the standard rate item.
      Create an instance of the TaxClassRate class. Set its Id property to Guid.Empty. Then set its Name property to “standardrate”. Set the Rate and the TaxExempt properties to the desired values. Finally add the standard rate item to the list.
    3. Create a TaxClassRate item for each available tax class.
      First, get all the available tax classes. Then iterate through them and create an instance of the TaxClassRate for each item. Set the Id and the Name properties of the TaxClassRate instance to the values of the Id and Title properties of the TaxClass instance. Add each of the TaxClassRate instances to the list.
    4. Serialize the List of TaxClassRate objects.
      To serialize the list, use the System.Web.Script.Serialization.JavaScriptSerializer class.
    5. Set the tax class rates.
      Set the serialized string to the TaxRates property of the Tax instance. You can access the tax class rates as a collection through the TaxRateCollection property of the Tax instance. Note that you can only modify the collection by modifying the string value of the TaxRates property. For more information, see Setting the tax class rates.
  5. Save the changes.
    To save the changes, call the SaveChanges method of the orders manager.

Here is an example:

public static void CreateTax(string title, AddressType addresType, string country, decimal standardRateValue, bool standartRateTaxExempt, bool applyTaxToShipping)
{
    OrdersManager ordersManager = OrdersManager.GetManager();
 
    Tax tax = ordersManager.CreateTax();
 
    //Set the properties.
    tax.Title = title;
    tax.TaxRate = standardRateValue;
    tax.Country = country;
    tax.AddressType = addresType;
    tax.ApplyTaxToShipping = applyTaxToShipping;
 
    List<TaxClassRate> taxClassRates = new List<TaxClassRate>();
 
    //Add the standard tax rate.
    TaxClassRate standartRate = new TaxClassRate()
    {
        Id = Guid.Empty,
        Name = "standardrate",
        Rate = standardRateValue,
        TaxExempt = standartRateTaxExempt,
    };
 
    taxClassRates.Add(standartRate);
 
    //Add the other tax rates.
    List<TaxClass> taxClasses = ordersManager.GetTaxClasses().ToList();
 
    foreach (TaxClass taxClass in taxClasses)
    {
        TaxClassRate taxClassRate = new TaxClassRate()
        {
            Id = taxClass.Id,
            Name = taxClass.Title
        };
 
        taxClassRates.Add(taxClassRate);
    }
 
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string taxRates = serializer.Serialize(taxClassRates);
 
    tax.TaxRates = taxRates;
 
    ordersManager.SaveChanges();
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK