Removing product tax based on user role

Removing product tax based on user role

Posted on September 12, 2014 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

Sitefinity provides different options for configuring taxes based on location and and tax class for products. Some cases require a user to be exempt from taxes, for example in a B2B scenario.

This can easily be achieved by creating a new tax class for products that is tax exempt. It will not be applied to any products, we need to be able to modify the products in the cart to use this class.

TaxClass

A simple widget is placed on the checkout page that will perform the following:

  • Check if the user is the tax exempt role
  • Get the cart id from the cart cookie
  • Change the tax class for all products in the cart to the exempt class effectively removing the tax for this order.

The control can be placed on the Checkout page to ensure that when the user is done shopping, the taxes will be altered accordingly.

 Here is the code of the control:

using System;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Ecommerce.Orders.Model;
using Telerik.Sitefinity.Modules.Ecommerce;
using Telerik.Sitefinity.Modules.Ecommerce.Orders;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Claims;
using Telerik.Sitefinity.Security.Model;
using Telerik.Sitefinity.Services;
 
namespace SitefinityWebApp
{
    public partial class RemoveTax : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var currentUser = ClaimsManager.GetCurrentIdentity();
            var exempt = IsUserInRole(currentUser.Name, "Exempt");
            HttpCookie shoppingCartCookie = SystemManager.CurrentHttpContext.Request.Cookies[EcommerceConstants.OrdersConstants.ShoppingCartIdCookieName];
            if (exempt && shoppingCartCookie!=null)
            {
                var oMan = OrdersManager.GetManager();
                 
                TaxClass taxClass = oMan.GetTaxClasses().Where(t => t.Title == "Exempt").SingleOrDefault();
               var cartId = new Guid(shoppingCartCookie.Value);
               var cartOrder = oMan.GetCartOrder(cartId);
               var details = oMan.GetCartDetails().Where(d => d.Parent.Id == cartOrder.Id);
               foreach (var item in details)
               {
                   item.TaxClassId = taxClass.Id;
                   item.TaxRate = 0;
               }
               cartOrder.EffectiveTaxRate = 0;
               cartOrder.Tax = 0;
               oMan.SaveChanges();
            }
        }
 
        public static bool IsUserInRole(string userName, string roleName)
        {
            bool isUserInRole = false;
  
            UserManager userManager = UserManager.GetManager();
            RoleManager roleManager = RoleManager.GetManager("Default");
  
            bool userExists = userManager.UserExists(userName);
            bool roleExists = roleManager.RoleExists(roleName);
  
            if (userExists && roleExists)
            {
                User user = userManager.GetUser(userName);
                isUserInRole = roleManager.IsUserInRole(user.Id, roleName);
            }
  
            return isUserInRole;
        }
    }
}

 You can also find the full sample on Github:Remove Tax.

Atanas Valchev

Atanas Valchev is a Tech Support Engineer at Telerik. He joined the Sitefinity Support team in March 2012.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation