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.
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.