示例#1
0
        public virtual IActionResult EstimateShipping([FromQuery] ProductEstimateShippingModel model, IFormCollection form)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            var product = _productService.GetProductById(model.ProductId);

            if (product == null || product.Deleted || !product.Published)
            {
                return(NotFound());
            }

            var errors = new List <string>();

            if (string.IsNullOrEmpty(model.ZipPostalCode))
            {
                errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.ZipPostalCode.Required"));
            }

            if (model.CountryId == null || model.CountryId == 0)
            {
                errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.Country.Required"));
            }

            if (errors.Count > 0)
            {
                return(BadRequest(new { Errors = errors }));
            }

            var wrappedProduct = new ShoppingCartItem()
            {
                StoreId            = _storeContext.CurrentStore.Id,
                ShoppingCartTypeId = (int)ShoppingCartType.ShoppingCart,
                CustomerId         = _workContext.CurrentCustomer.Id,
                ProductId          = product.Id,
                CreatedOnUtc       = DateTime.UtcNow
            };

            var addToCartWarnings = new List <string>();

            //customer entered price
            wrappedProduct.CustomerEnteredPrice = _productAttributeParser.ParseCustomerEnteredPrice(product, form);

            //entered quantity
            wrappedProduct.Quantity = _productAttributeParser.ParseEnteredQuantity(product, form);

            //product and gift card attributes
            wrappedProduct.AttributesXml = _productAttributeParser.ParseProductAttributes(product, form, addToCartWarnings);

            //rental attributes
            _productAttributeParser.ParseRentalDates(product, form, out var rentalStartDate, out var rentalEndDate);
            wrappedProduct.RentalStartDateUtc = rentalStartDate;
            wrappedProduct.RentalEndDateUtc   = rentalEndDate;

            var modelResult = _shoppingCartModelFactory.PrepareEstimateShippingResultModel(new [] { wrappedProduct }, model.CountryId, model.StateProvinceId, model.ZipPostalCode);

            return(Json(modelResult));
        }