Пример #1
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="Error">Error</param>
        /// <returns>Shipping options</returns>
        public static ShippingOptionCollection GetShippingOptions(ShoppingCart Cart, Customer customer, Address ShippingAddress, ref string Error)
        {
            if (Cart == null)
            {
                throw new ArgumentNullException("Cart");
            }

            bool isFreeShipping = IsFreeShipping(Cart, customer);

            ShipmentPackage ShipmentPackage = CreateShipmentPackage(Cart, customer, ShippingAddress);
            ShippingRateComputationMethod activeShippingRateComputationMethod = ActiveShippingRateComputationMethod;

            if (activeShippingRateComputationMethod == null)
            {
                throw new NopException("Shipping rate computation method could not be loaded");
            }
            IShippingRateComputationMethod iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(activeShippingRateComputationMethod.ClassName)) as IShippingRateComputationMethod;

            ShippingOptionCollection shippingOptions = iShippingRateComputationMethod.GetShippingOptions(ShipmentPackage, ref Error);

            decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(Cart, customer);

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

            if (isFreeShipping)
            {
                shippingOptions.ForEach(so => so.Rate = Decimal.Zero);
            }

            shippingOptions.ForEach(so => so.Rate = Math.Round(so.Rate, 2));

            return(shippingOptions);
        }
Пример #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);
        }