Esempio n. 1
0
        /// <summary>
        /// Gets shopping cart shipping total
        /// </summary>
        /// <param name="cart">Cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="includingTax">A value indicating whether calculated price should include tax</param>
        /// <param name="appliedDiscount">Applied discount</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping total</returns>
        public static decimal?GetShoppingCartShippingTotal(ShoppingCart cart,
                                                           Customer customer, bool includingTax, out Discount appliedDiscount, ref string error)
        {
            decimal?shippingTotalWithoutDiscount   = null;
            decimal?shippingTotalWithDiscount      = null;
            decimal?shippingTotalWithDiscountTaxed = null;

            appliedDiscount = null;

            bool isFreeShipping = IsFreeShipping(cart, customer);

            if (isFreeShipping)
            {
                return(decimal.Zero);
            }

            ShippingOption lastShippingOption = null;

            if (customer != null)
            {
                lastShippingOption = customer.LastShippingOption;
            }

            if (lastShippingOption != null)
            {
                //use last shipping option (get from cache)
                //we have already discounted cache value
                shippingTotalWithDiscount = lastShippingOption.Rate;
                appliedDiscount           = DiscountManager.GetDiscountById(lastShippingOption.AppliedDiscountId);
            }
            else
            {
                //use fixed rate (if possible)
                Address shippingAddress = null;
                if (customer != null)
                {
                    shippingAddress = customer.ShippingAddress;
                }
                var ShipmentPackage = CreateShipmentPackage(cart, customer, shippingAddress);
                var shippingRateComputationMethods = ShippingRateComputationMethodManager.GetAllShippingRateComputationMethods(false);
                if (shippingRateComputationMethods.Count == 0)
                {
                    throw new NopException("Shipping rate computation method could not be loaded");
                }

                if (shippingRateComputationMethods.Count == 1)
                {
                    var shippingRateComputationMethod  = shippingRateComputationMethods[0];
                    var iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(shippingRateComputationMethod.ClassName)) as IShippingRateComputationMethod;

                    decimal?fixedRate = iShippingRateComputationMethod.GetFixedRate(ShipmentPackage);
                    if (fixedRate.HasValue)
                    {
                        decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(cart, customer);
                        shippingTotalWithoutDiscount = fixedRate.Value + additionalShippingCharge;
                        shippingTotalWithoutDiscount = Math.Round(shippingTotalWithoutDiscount.Value, 2);
                        decimal shippingTotalDiscount = GetShippingDiscount(customer, shippingTotalWithoutDiscount.Value, out appliedDiscount);
                        shippingTotalWithDiscount = shippingTotalWithoutDiscount.Value - shippingTotalDiscount;
                        if (shippingTotalWithDiscount.Value < decimal.Zero)
                        {
                            shippingTotalWithDiscount = decimal.Zero;
                        }
                    }
                }
            }

            if (!shippingTotalWithDiscount.HasValue)
            {
                error = "Shipping total could not be calculated";
            }
            else
            {
                shippingTotalWithDiscountTaxed = TaxManager.GetShippingPrice(shippingTotalWithDiscount.Value,
                                                                             includingTax,
                                                                             customer,
                                                                             ref error);

                shippingTotalWithDiscountTaxed = Math.Round(shippingTotalWithDiscountTaxed.Value, 2);
            }

            return(shippingTotalWithDiscountTaxed);
        }
Esempio n. 2
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="cart">Shopping cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="shippingAddress">Shipping address</param>
        /// <param name="allowedShippingRateComputationMethodId">Allowed shipping rate computation method identifier; null to load shipping options of all methods</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public static ShippingOptionCollection GetShippingOptions(ShoppingCart cart,
                                                                  Customer customer, Address shippingAddress,
                                                                  int?allowedShippingRateComputationMethodId, ref string error)
        {
            if (cart == null)
            {
                throw new ArgumentNullException("cart");
            }

            var shippingOptions = new ShippingOptionCollection();

            bool isFreeShipping = IsFreeShipping(cart, customer);

            var ShipmentPackage = CreateShipmentPackage(cart, customer, shippingAddress);
            var shippingRateComputationMethods = ShippingRateComputationMethodManager.GetAllShippingRateComputationMethods(false);

            if (shippingRateComputationMethods.Count == 0)
            {
                throw new NopException("Shipping rate computation method could not be loaded");
            }

            foreach (var srcm in shippingRateComputationMethods)
            {
                if (allowedShippingRateComputationMethodId.HasValue &&
                    allowedShippingRateComputationMethodId.Value > 0 &&
                    allowedShippingRateComputationMethodId.Value != srcm.ShippingRateComputationMethodId)
                {
                    continue;
                }

                var iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(srcm.ClassName)) as IShippingRateComputationMethod;

                var shippingOptions2 = iShippingRateComputationMethod.GetShippingOptions(ShipmentPackage, ref error);
                if (shippingOptions2 != null)
                {
                    foreach (var so2 in shippingOptions2)
                    {
                        so2.ShippingRateComputationMethodId = srcm.ShippingRateComputationMethodId;
                        shippingOptions.Add(so2);
                    }
                }
            }

            if (shippingOptions.Count == 0)
            {
                error = "Shipping options could not be loaded";
            }

            //additional shipping charges
            decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(cart, customer);

            shippingOptions.ForEach(so => so.Rate += additionalShippingCharge);

            //discounts
            foreach (var so in shippingOptions)
            {
                decimal rateWithoutDiscount = Math.Round(so.Rate, 2);
                decimal rateWithDiscount    = decimal.Zero;

                Discount rateDiscount       = null;
                decimal  rateDiscountAmount = GetShippingDiscount(customer, rateWithoutDiscount, out rateDiscount);
                rateWithDiscount = rateWithoutDiscount - rateDiscountAmount;
                if (rateWithDiscount < decimal.Zero)
                {
                    rateWithDiscount = decimal.Zero;
                }

                rateWithDiscount = Math.Round(rateWithDiscount, 2);

                so.Rate = rateWithDiscount;
                if (rateDiscount != null)
                {
                    so.AppliedDiscountId = rateDiscount.DiscountId;
                }
            }

            //free shipping
            if (isFreeShipping)
            {
                shippingOptions.ForEach(so => so.Rate = decimal.Zero);
            }

            return(shippingOptions);
        }
Esempio n. 3
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="cart">Shopping cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="shippingAddress">Shipping address</param>
        /// <param name="allowedShippingRateComputationMethodId">Filter by shipping rate computation method identifier; null to load shipping options of all shipping rate computation methods</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public static List <ShippingOption> GetShippingOptions(ShoppingCart cart,
                                                               Customer customer, Address shippingAddress,
                                                               int?allowedShippingRateComputationMethodId, ref string error)
        {
            if (cart == null)
            {
                throw new ArgumentNullException("cart");
            }

            var shippingOptions = new List <ShippingOption>();

            bool isFreeShipping = IsFreeShipping(cart, customer);

            //create a package
            var shipmentPackage = CreateShipmentPackage(cart, customer, shippingAddress);
            var shippingRateComputationMethods = ShippingRateComputationMethodManager.GetAllShippingRateComputationMethods(false);

            if (shippingRateComputationMethods.Count == 0)
            {
                throw new NopException("Shipping rate computation method could not be loaded");
            }

            //get shipping options
            foreach (var srcm in shippingRateComputationMethods)
            {
                if (allowedShippingRateComputationMethodId.HasValue &&
                    allowedShippingRateComputationMethodId.Value > 0 &&
                    allowedShippingRateComputationMethodId.Value != srcm.ShippingRateComputationMethodId)
                {
                    continue;
                }

                var iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(srcm.ClassName)) as IShippingRateComputationMethod;

                var shippingOptions2 = iShippingRateComputationMethod.GetShippingOptions(shipmentPackage, ref error);
                if (shippingOptions2 != null)
                {
                    foreach (var so2 in shippingOptions2)
                    {
                        so2.ShippingRateComputationMethodId = srcm.ShippingRateComputationMethodId;
                        shippingOptions.Add(so2);
                    }
                }
            }

            //no shipping options loaded
            if (shippingOptions.Count == 0 && String.IsNullOrEmpty(error))
            {
                error = "Shipping options could not be loaded";
            }

            //additional shipping charges
            decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(cart, customer);

            shippingOptions.ForEach(so => so.Rate += additionalShippingCharge);

            //free shipping
            if (isFreeShipping)
            {
                shippingOptions.ForEach(so => so.Rate = decimal.Zero);
            }

            return(shippingOptions);
        }