Adding products to cart while anonymous

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
15 years ago
Hello, here's a good one on 1.04:

Clicking the Add to Cart button while anonymous generates the following

-------------------------------------------
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 61:             decimal? shippingTotal = decimal.Zero;
Line 62:
Line 63:             CustomerRoleCollection customerRoles = customer.CustomerRoles;
Line 64:             foreach (CustomerRole customerRole in customerRoles)
Line 65:                 if (customerRole.FreeShipping)


Source File: C:\Users\juju\Documents\My WebProjects\nopCommerce\NopCommerce\Nop.Common\BLL\Managers\ShippingManager.cs    Line: 63

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   NopSolutions.NopCommerce.Common.BLL.Managers.ShippingManager.GetShoppingCartShippingTotal(ShoppingCartCollection Cart, Customer customer, ShippingInfo shippingInfo, Boolean includeDiscounts, Country CountryFrom, StateProvince StateProvinceFrom, String ZipPostalCodeFrom) in C:\Users\juju\Documents\My WebProjects\nopCommerce\NopCommerce\Nop.Common\BLL\Managers\ShippingManager.cs:63
   NopSolutions.NopCommerce.Common.BLL.Managers.ShippingManager.GetShoppingCartShippingTotal(ShoppingCartCollection Cart, Customer customer, ShippingInfo shippingInfo, Boolean includeDiscounts) in C:\Users\juju\Documents\My WebProjects\nopCommerce\NopCommerce\Nop.Common\BLL\Managers\ShippingManager.cs:45
   NopSolutions.NopCommerce.Web.Modules.OrderSummary.BindData() in c:\Users\juju\Documents\My WebProjects\nopCommerce\NopCommerce\NopCommerceWeb\Modules\OrderSummary.ascx.cs:77
   NopSolutions.NopCommerce.Web.ShoppingCartPage.Page_Load(Object sender, EventArgs e) in c:\Users\juju\Documents\My WebProjects\nopCommerce\NopCommerce\NopCommerceWeb\ShoppingCart.aspx.cs:43
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

-------------------------------------------

When you are looged in, this problem goes away.

Thanks
15 years ago
Hi

1) Modify GetShoppingCartShippingTotal procedure in ShippingManager.cs
/// <summary>
        /// Gets shopping cart shipping total
        /// </summary>
        /// <param name="Cart">Cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="shippingInfo">Shipping Info</param>
        /// <param name="includeDiscounts">A value indicating whether include discounts or not for shipping computation</param>
        /// <param name="CountryFrom">Shipped from country</param>
        /// <param name="StateProvinceFrom">Shipped from state/province</param>
        /// <param name="ZipPostalCodeFrom">Shipped from zip/postal code</param>
        /// <returns>Shipping total</returns>
public static decimal? GetShoppingCartShippingTotal(ShoppingCartCollection Cart, Customer customer, ShippingInfo shippingInfo, bool includeDiscounts, Country CountryFrom, StateProvince StateProvinceFrom, string ZipPostalCodeFrom)
        {
            decimal? shippingTotal = decimal.Zero;

            if (customer != null)
            {
                CustomerRoleCollection customerRoles = customer.CustomerRoles;
                foreach (CustomerRole customerRole in customerRoles)
                    if (customerRole.FreeShipping)
                        return decimal.Zero;
            }

            bool shoppingCartRequiresShipping = ShoppingCartRequiresShipping(Cart);
            if (!shoppingCartRequiresShipping)
                return decimal.Zero;

            decimal orderSubTotal = ShoppingCartManager.GetShoppingCartSubTotal(Cart, includeDiscounts);
            if (SettingManager.GetSettingValueBoolean("Shipping.FreeShippingOverX.Enabled"))
            {
                decimal freeShippingOverX = SettingManager.GetSettingValueDecimalNative("Shipping.FreeShippingOverX.Value");
                if (orderSubTotal > freeShippingOverX)
                    return decimal.Zero;
            }
            
            ShippingRateComputationMethod activeShippingRateComputationMethod = ActiveShippingRateComputationMethod;
            if (activeShippingRateComputationMethod == null)
                throw new Exception("Shipping rate computation method could not be loaded");
            IShippingRateComputationMethod iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(activeShippingRateComputationMethod.ClassName)) as IShippingRateComputationMethod;

            shippingTotal = iShippingRateComputationMethod.GetShippingRate(Cart, shippingInfo, includeDiscounts, CountryFrom, StateProvinceFrom, ZipPostalCodeFrom);
            return shippingTotal;
        }



2) Modify GetShoppingCartTaxTotal procedure in TaxManager.cs
/// <summary>
        /// Gets tax total
        /// </summary>
        /// <param name="Cart">Current shopping cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="billingAddress">Billing address</param>
        /// <param name="includeDiscounts">A value indicating whether include discounts or not for tax computation</param>
        /// <returns>Tax total;Null if tax total couldn't be calculated now</returns>
        public static decimal? GetShoppingCartTaxTotal(ShoppingCartCollection Cart, Customer customer, Address billingAddress, bool includeDiscounts)
        {
            decimal taxTotal = decimal.Zero;

            if (customer != null)
            {
                CustomerRoleCollection customerRoles = customer.CustomerRoles;
                foreach (CustomerRole customerRole in customerRoles)
                    if (customerRole.TaxExempt)
                        return decimal.Zero;

                if (billingAddress == null)
                    return null;
            }

            foreach (ShoppingCart shoppingCart in Cart)
            {
                ProductVariant productVariant = shoppingCart.ProductVariant;
                if (productVariant != null)
                {
                    decimal shoppingCartItemSubTotal = decimal.Zero;
                    if (includeDiscounts)
                        shoppingCartItemSubTotal = shoppingCart.SubTotalWithDiscount;
                    else
                        shoppingCartItemSubTotal = shoppingCart.SubTotalWithoutDiscount;
                    decimal taxPercentage = GetTaxRate(billingAddress, productVariant.TaxCategoryID);
                    taxTotal += shoppingCartItemSubTotal * (taxPercentage / 100M);
                }
            }

            return taxTotal;
        }
15 years ago
Thank  you for your reply.

I noticed it has been corrected with the 1.05 package. I will upgrade to that package instead.

More power
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.