Пример #1
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="getShippingOptionRequest">A request for getting shipping options</param>
        /// <returns>Represents a response of getting shipping rate options</returns>
        public async Task <GetShippingOptionResponse> GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest)
        {
            if (getShippingOptionRequest == null)
            {
                throw new ArgumentNullException(nameof(getShippingOptionRequest));
            }

            var response = new GetShippingOptionResponse();

            if (getShippingOptionRequest.Items == null || getShippingOptionRequest.Items.Count == 0)
            {
                response.AddError("No shipment items");
                return(response);
            }

            string restrictByCountryId = (getShippingOptionRequest.ShippingAddress != null && !String.IsNullOrEmpty(getShippingOptionRequest.ShippingAddress.CountryId)) ? getShippingOptionRequest.ShippingAddress.CountryId : "";
            var    shippingMethods     = await _shippingMethodService.GetAllShippingMethods(restrictByCountryId, getShippingOptionRequest.Customer);

            foreach (var shippingMethod in shippingMethods)
            {
                var shippingOption = new ShippingOption
                {
                    Name        = shippingMethod.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
                    Description = shippingMethod.GetTranslation(x => x.Description, _workContext.WorkingLanguage.Id),
                    Rate        = await _currencyService.ConvertFromPrimaryStoreCurrency(GetRate(shippingMethod.Id), _workContext.WorkingCurrency)
                };
                response.ShippingOptions.Add(shippingOption);
            }

            return(response);
        }
Пример #2
0
        public async Task <IActionResult> Methods(DataSourceRequest command)
        {
            var shippingMethodsModel = (await _shippingMethodService.GetAllShippingMethods())
                                       .Select(x => x.ToModel())
                                       .ToList();
            var gridModel = new DataSourceResult {
                Data  = shippingMethodsModel,
                Total = shippingMethodsModel.Count
            };

            return(Json(gridModel));
        }
Пример #3
0
        public async Task <IActionResult> MethodRestrictions()
        {
            var model          = new PaymentMethodRestrictionModel();
            var paymentMethods = _paymentService.LoadAllPaymentMethods();
            var countries      = await _countryService.GetAllCountries(showHidden : true);

            var shippings = await _shippingMethodService.GetAllShippingMethods();

            foreach (var pm in paymentMethods)
            {
                model.AvailablePaymentMethods.Add(await pm.ToModel());
            }
            foreach (var c in countries)
            {
                model.AvailableCountries.Add(c.ToModel());
            }
            foreach (var s in shippings)
            {
                model.AvailableShippingMethods.Add(new Models.Shipping.ShippingMethodModel()
                {
                    Id   = s.Id,
                    Name = s.Name
                });
            }

            foreach (var pm in paymentMethods)
            {
                var restictedCountries = _paymentService.GetRestrictedCountryIds(pm);
                foreach (var c in countries)
                {
                    bool resticted = restictedCountries.Contains(c.Id);
                    if (!model.Resticted.ContainsKey(pm.SystemName))
                    {
                        model.Resticted[pm.SystemName] = new Dictionary <string, bool>();
                    }
                    model.Resticted[pm.SystemName][c.Id] = resticted;
                }

                var restictedShipping = _paymentService.GetRestrictedShippingIds(pm);
                foreach (var s in shippings)
                {
                    bool resticted = restictedShipping.Contains(s.Name);
                    if (!model.RestictedShipping.ContainsKey(pm.SystemName))
                    {
                        model.RestictedShipping[pm.SystemName] = new Dictionary <string, bool>();
                    }
                    model.RestictedShipping[pm.SystemName][s.Name] = resticted;
                }
            }

            return(View(model));
        }
Пример #4
0
        public async Task <IActionResult> Configure(DataSourceRequest command)
        {
            if (!await _permissionService.Authorize(StandardPermission.ManageShippingSettings))
            {
                return(Content("Access denied"));
            }

            var rateModels = new List <FixedShippingRateModel>();

            foreach (var shippingMethod in await _shippingMethodService.GetAllShippingMethods())
            {
                rateModels.Add(new FixedShippingRateModel
                {
                    ShippingMethodId   = shippingMethod.Id,
                    ShippingMethodName = shippingMethod.Name,
                    Rate = GetShippingRate(shippingMethod.Id)
                });
            }

            var gridModel = new DataSourceResult
            {
                Data  = rateModels,
                Total = rateModels.Count
            };

            return(Json(gridModel));
        }
        public async Task <IActionResult> AddPopup()
        {
            var model = new ShippingByWeightModel();

            model.PrimaryStoreCurrencyCode = (await _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)).CurrencyCode;
            model.BaseWeightIn             = (await _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId)).Name;
            model.To = 1000000;

            var shippingMethods = await _shippingMethodService.GetAllShippingMethods();

            if (shippingMethods.Count == 0)
            {
                return(Content("No shipping methods can be loaded"));
            }

            //stores
            model.AvailableStores.Add(new SelectListItem {
                Text = "*", Value = " "
            });
            foreach (var store in await _storeService.GetAllStores())
            {
                model.AvailableStores.Add(new SelectListItem {
                    Text = store.Shortcut, Value = store.Id.ToString()
                });
            }
            //warehouses
            model.AvailableWarehouses.Add(new SelectListItem {
                Text = "*", Value = " "
            });
            foreach (var warehouses in await _warehouseService.GetAllWarehouses())
            {
                model.AvailableWarehouses.Add(new SelectListItem {
                    Text = warehouses.Name, Value = warehouses.Id.ToString()
                });
            }
            //shipping methods
            foreach (var sm in shippingMethods)
            {
                model.AvailableShippingMethods.Add(new SelectListItem {
                    Text = sm.Name, Value = sm.Id.ToString()
                });
            }
            //countries
            model.AvailableCountries.Add(new SelectListItem {
                Text = "*", Value = " "
            });
            var countries = await _countryService.GetAllCountries(showHidden : true);

            foreach (var c in countries)
            {
                model.AvailableCountries.Add(new SelectListItem {
                    Text = c.Name, Value = c.Id.ToString()
                });
            }
            //states
            model.AvailableStates.Add(new SelectListItem {
                Text = "*", Value = ""
            });

            return(View(model));
        }
Пример #6
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="getShippingOptionRequest">A request for getting shipping options</param>
        /// <returns>Represents a response of getting shipping rate options</returns>
        public async Task <GetShippingOptionResponse> GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest)
        {
            if (getShippingOptionRequest == null)
            {
                throw new ArgumentNullException(nameof(getShippingOptionRequest));
            }

            var response = new GetShippingOptionResponse();

            if (getShippingOptionRequest.Items == null || getShippingOptionRequest.Items.Count == 0)
            {
                response.AddError("No shipment items");
                return(response);
            }
            if (getShippingOptionRequest.ShippingAddress == null)
            {
                response.AddError("Shipping address is not set");
                return(response);
            }

            var storeId = getShippingOptionRequest.StoreId;

            if (String.IsNullOrEmpty(storeId))
            {
                storeId = _workContext.CurrentStore.Id;
            }
            string countryId       = getShippingOptionRequest.ShippingAddress.CountryId;
            string stateProvinceId = getShippingOptionRequest.ShippingAddress.StateProvinceId;

            //string warehouseId = getShippingOptionRequest.WarehouseFrom != null ? getShippingOptionRequest.WarehouseFrom.Id : "";

            string zip      = getShippingOptionRequest.ShippingAddress.ZipPostalCode;
            double subTotal = 0;
            var    priceCalculationService = _serviceProvider.GetRequiredService <IPricingService>();

            foreach (var packageItem in getShippingOptionRequest.Items)
            {
                if (packageItem.ShoppingCartItem.IsFreeShipping)
                {
                    continue;
                }

                var product = await _productService.GetProductById(packageItem.ShoppingCartItem.ProductId);

                if (product != null)
                {
                    subTotal += (await priceCalculationService.GetSubTotal(packageItem.ShoppingCartItem, product)).subTotal;
                }
            }

            double weight = await GetTotalWeight(getShippingOptionRequest);

            var shippingMethods = await _shippingMethodService.GetAllShippingMethods(countryId, _workContext.CurrentCustomer);

            foreach (var shippingMethod in shippingMethods)
            {
                double?rate = null;
                foreach (var item in getShippingOptionRequest.Items.GroupBy(x => x.ShoppingCartItem.WarehouseId).Select(x => x.Key))
                {
                    var _rate = await GetRate(subTotal, weight, shippingMethod.Id, storeId, item, countryId, stateProvinceId, zip);

                    if (_rate.HasValue)
                    {
                        if (rate == null)
                        {
                            rate = 0;
                        }

                        rate += _rate.Value;
                    }
                }

                if (rate != null && rate.HasValue)
                {
                    var shippingOption = new ShippingOption();
                    shippingOption.Name        = shippingMethod.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);
                    shippingOption.Description = shippingMethod.GetTranslation(x => x.Description, _workContext.WorkingLanguage.Id);
                    shippingOption.Rate        = await _currencyService.ConvertFromPrimaryStoreCurrency(rate.Value, _workContext.WorkingCurrency);

                    response.ShippingOptions.Add(shippingOption);
                }
            }


            return(response);
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="getShippingOptionRequest">A request for getting shipping options</param>
        /// <returns>Represents a response of getting shipping rate options</returns>
        public async Task <GetShippingOptionResponse> GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest)
        {
            if (getShippingOptionRequest == null)
            {
                throw new ArgumentNullException("getShippingOptionRequest");
            }

            var response = new GetShippingOptionResponse();

            if (getShippingOptionRequest.Items == null || getShippingOptionRequest.Items.Count == 0)
            {
                response.AddError("No shipment items");
                return(response);
            }
            if (getShippingOptionRequest.ShippingAddress == null)
            {
                response.AddError("Shipping address is not set");
                return(response);
            }

            var storeId = getShippingOptionRequest.StoreId;

            if (String.IsNullOrEmpty(storeId))
            {
                storeId = _storeContext.CurrentStore.Id;
            }
            string  countryId       = getShippingOptionRequest.ShippingAddress.CountryId;
            string  stateProvinceId = getShippingOptionRequest.ShippingAddress.StateProvinceId;
            string  warehouseId     = getShippingOptionRequest.WarehouseFrom != null ? getShippingOptionRequest.WarehouseFrom.Id : "";
            string  zip             = getShippingOptionRequest.ShippingAddress.ZipPostalCode;
            decimal subTotal        = decimal.Zero;

            foreach (var packageItem in getShippingOptionRequest.Items)
            {
                if (packageItem.ShoppingCartItem.IsFreeShipping)
                {
                    continue;
                }
                //TODO we should use getShippingOptionRequest.Items.GetQuantity() method to get subtotal
                subTotal += (await _priceCalculationService.GetSubTotal(packageItem.ShoppingCartItem)).subTotal;
            }
            decimal weight = await _shippingService.GetTotalWeight(getShippingOptionRequest);

            var shippingMethods = await _shippingMethodService.GetAllShippingMethods(countryId, _workContext.CurrentCustomer);

            foreach (var shippingMethod in shippingMethods)
            {
                decimal?rate = await GetRate(subTotal, weight, shippingMethod.Id,
                                             storeId, warehouseId, countryId, stateProvinceId, zip);

                if (rate.HasValue)
                {
                    var shippingOption = new ShippingOption();
                    shippingOption.Name        = shippingMethod.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id);
                    shippingOption.Description = shippingMethod.GetLocalized(x => x.Description, _workContext.WorkingLanguage.Id);
                    shippingOption.Rate        = rate.Value;
                    response.ShippingOptions.Add(shippingOption);
                }
            }


            return(response);
        }