Hi DW,
We have this code running on a site to set VAT to 0% if a specific country in EU is selected, and a VAT/EAN number is supplied.
[Dynamicweb.Extensibility.Subscribe( Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStep )]
public class OCDBeforeRenderingNewStep : Dynamicweb.Extensibility.NotificationSubscriber
{
public override void OnNotify( string notification, Dynamicweb.Extensibility.NotificationArgs args )
{
Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStepArgs afterCustomerCountryIsSetArgs = args as Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStepArgs;
// Country must be set for this notification to do anything
if ( afterCustomerCountryIsSetArgs.Order.CustomerCountryCode == null ) {
return;
}
// If we're not on the correct cart page, don't run the code
string gsCartPageID = Dynamicweb.Base.GetGs( "/Globalsettings/Co3/EventCartPageId" );
string pageID = Dynamicweb.Base.Request("id", true);
if ( pageID.Equals( gsCartPageID ) == false )
{
return;
}
string orderCountryCode = afterCustomerCountryIsSetArgs.Order.CustomerCountryCode.ToUpper();
if ( string.IsNullOrEmpty( orderCountryCode ) == false )
{
Dynamicweb.eCommerce.International.CountryCollection countries = Dynamicweb.eCommerce.International.Country.getCountries( orderCountryCode );
double countryVat = countries[0].Vat;
Dynamicweb.eCommerce.Orders.Order o = afterCustomerCountryIsSetArgs.Order;
// Test if VAT number is set and country is in the EU
if ( string.IsNullOrEmpty( afterCustomerCountryIsSetArgs.Order.CustomerVatRegNumber ) == false )
{
// Set VAT to 0
if ( Helper.IsEuMemberCountry( orderCountryCode ) == true && orderCountryCode.Equals( "DK" ) == false )
{
UpdateVATOnOrder( ref o, 0 );
}
// Set VAT to countries standard VAT
else
{
UpdateVATOnOrder( ref o, countryVat );
}
}
// If VAT number is not present, calculate VAT again,since it could have been reset earlier.
else
{
UpdateVATOnOrder( ref o, countryVat );
}
}
}
private void UpdateVATOnOrder( ref Dynamicweb.eCommerce.Orders.Order order, double newVAT )
{
// Do nothing if cart is null
if ( order == null )
return;
// Loop orderlines and change unitprice.
foreach ( Dynamicweb.eCommerce.Orders.OrderLine ol in order.ProductOrderLines )
{
var olPrice = ol.UnitPrice;
SetNewVat( olPrice, newVAT );
ol.SetUnitPrice( olPrice, true );
ol.Type = ((int)Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.Fixed).ToString();
}
order.ClearCachedPrices();
var payFeeWithoutVat = order.PaymentFee;
SetNewVat( payFeeWithoutVat, newVAT );
var shipFeeWithoutVat = order.ShippingFee;
SetNewVat( shipFeeWithoutVat, newVAT );
var newOrderPrice = order.PriceBeforeFees.Add( payFeeWithoutVat ).Add( shipFeeWithoutVat );
// set new orderprice
order.AllowOverridePrices = true;
order.Price.PriceWithoutVAT = newOrderPrice.PriceWithoutVAT;
order.Price.PriceWithVAT = newOrderPrice.PriceWithVAT;
order.Price.VAT = (newOrderPrice.VAT >= 0 ? newOrderPrice.VAT : 0);
order.Price.VATPercent = (newOrderPrice.VATPercent >= 0 ? newOrderPrice.VATPercent : 0);
}
private void SetNewVat( Dynamicweb.eCommerce.Prices.PriceInfo price, double newVat )
{
price.PriceWithVAT = price.PriceWithoutVAT * (1 + (newVat / 100));
price.VAT = price.PriceWithVAT - price.PriceWithoutVAT;
price.VATPercent = newVat;
}
}
Last week we updated to 8.4.1.8 and now the code above removes Discounts from the cart. Do you have any idea what part of the code does this, and what i should change to avoid it.
// Martin