Developer forum

Forum » Development » VAT provider problem

VAT provider problem

Keld Lauge Gøtterup
Reply

im trying to make a VAT provider but i cant seem to figure out why i get the following error


code looks like this:

public class VatProvider1 : Dynamicweb.eCommerce.Orders.VatProvider
    {
        public override double FindVatPercent(double DefaultVatPercent, Product Product)
        {

            var cart = Dynamicweb.eCommerce.Common.Context.Cart;
            if (cart == null) return DefaultVatPercent;


            if (cart.CustomerCountryCode == null || cart.DeliveryCountryCode == null)
                return base.FindVatPercent(DefaultVatPercent, Product);


            if ((cart.CustomerCountryCode.ToLower() == "dk" || cart.DeliveryCountryCode.ToLower() == "dk"))
            {

                return base.FindVatPercent(DefaultVatPercent, Product);
            }
            try
            {
                if (Product.VatGroup.GroupName != null && Product.VatGroup.GroupName == "Kurser")
                {
                    return base.FindVatPercent(DefaultVatPercent, Product);
                }
            }
            catch(Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(ex.Message);
                System.Web.HttpContext.Current.Response.Write(ex.StackTrace);
            }


            return 0;   

        }
    }



Im getting an 2Object Reference not set to an insance of an object." in this codeline

 

if (Product.VatGroup.GroupName != null && Product.VatGroup.GroupName == "Kurser") 



Anyone have an idea as to why i get this error and possibly what i have to do instead

 


Replies

 
Martin Nielsen
Reply

Maybe Product.VatGroup is null

 

Try this code:

if ( Product.VatGroup != null &&  Product.VatGroup.GroupName != null && Product.VatGroup.GroupName == "Kurser") 

 

// Martin

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

It should not be possible for VatGroup to be null as Product.VatGroup will new up an instance of a VatGroup if one does not exist.

 

Maybe you could try and debug your provider and check which object is null?

 

- Jeppe

 
Keld Lauge Gøtterup
Reply

when i try and have a single product in the cart and i change customercountry from DK to random.

it writes out the "P = null" 21 times as written in the underlying code and i fail to understand why it would make 21 calls to the provider for 1 product which it observes as null.

 

public class VatProvider1 : Dynamicweb.eCommerce.Orders.VatProvider
    {
        public override double FindVatPercent(double DefaultVatPercent, Product Product)
        {

            var cart = Dynamicweb.eCommerce.Common.Context.Cart;
            if (cart == null) return DefaultVatPercent;


            if (cart.CustomerCountryCode == null || cart.DeliveryCountryCode == null)
                return base.FindVatPercent(DefaultVatPercent, Product);


            if ((cart.CustomerCountryCode.ToLower() == "dk" || cart.DeliveryCountryCode.ToLower() == "dk"))
            {

                return base.FindVatPercent(DefaultVatPercent, Product);
            }
            if (Product != null)
            {
                System.Web.HttpContext.Current.Response.Write("P = null <br />");
            }
            try
            {
                if (Product != null && Product.VatGroup != null && Product.VatGroup.GroupName != null && Product.VatGroup.GroupName == "Kurser")
                {
                    return base.FindVatPercent(DefaultVatPercent, Product);
                }
            }
            catch(Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(ex.Message + " <br />");
                System.Web.HttpContext.Current.Response.Write(ex.StackTrace + " <br />");
                System.Web.HttpContext.Current.Response.Write(ex.InnerException + " <br />");
            }


            return 0;   

        }
    }






 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

If I read your debugging code correctly, then the product is in fact not null; because of

if (Product != null)

It's not unusual for providers and extenders to be invoked multiple times in a standard Ecom flow, but I agree though that 21 times for a single product seems a bit excessive.

 

I'll try your VatProvider locally to see if I can reproduce what you're experiencing here. Which version of Dynamicweb are you running?

 

- Jeppe

 
Keld Lauge Gøtterup
Reply

Content version 20.2.3.17
Assembly versioner
Dynamicweb.dll 8.2.3.17

 

 
Keld Lauge Gøtterup
Reply

and you are of course correct in the null not null part of my code ;)


i changed it and now it spits out this... so only once is it null in these many calls
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P != null
P = null
P != null

 

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Keld,

 

I've now tested your VatProvider code on a local 8.2.3, and I get the same result as you.

 

As I mentioned before, it's not unusual to see providers being invoked multiple times and the same goes for the VatProvider. Ecom will ask the VatProvider each time it needs a price for the product. That is, every time a price is not cached, has a currency mismatch, or is in some other way unusable.

 

As for the one instance where "P = null", this is the result of a calculation to find the orderline price with discounts applied. Since the price is calculated without a product (being a hybrid of a product and a discount), the product argument passed to the VatProvider is therefore null.

 

There are ways we can fix this, like only invoking the VatProvider when a product is available or adding other overridable methods to a VatProvider, but this is something we need to discuss internally. We need to consider the potential breaking changes that would arise from these changes before we make them.

 

For now, the best way to handle this is to verify that Product is not null before using the variable.

 

Hope this helps :)

 

- Jeppe

Votes for this answer: 1

 

You must be logged in to post in the forum