Esempio n. 1
0
        public async Task CanPrepareEstimateShippingResultModel()
        {
            var model = await _shoppingCartModelFactory.PrepareEstimateShippingResultModelAsync(new List <ShoppingCartItem> {
                _shoppingCartItem
            }, new EstimateShippingModel(), true);

            model.Errors.Any().Should().BeFalse();
        }
        /// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task <IActionResult> EstimateShipping([FromQuery] ProductDetailsModel.ProductEstimateShippingModel model, IFormCollection form)
        {
            if (model == null)
            {
                model = new ProductDetailsModel.ProductEstimateShippingModel();
            }

            var errors = new List <string>();

            if (!_shippingSettings.EstimateShippingCityNameEnabled && string.IsNullOrEmpty(model.ZipPostalCode))
            {
                errors.Add(await _localizationService.GetResourceAsync("Shipping.EstimateShipping.ZipPostalCode.Required"));
            }

            if (_shippingSettings.EstimateShippingCityNameEnabled && string.IsNullOrEmpty(model.City))
            {
                errors.Add(await _localizationService.GetResourceAsync("Shipping.EstimateShipping.City.Required"));
            }

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

            if (errors.Count > 0)
            {
                return(Json(new
                {
                    Success = false,
                    Errors = errors
                }));
            }

            var product = await _productService.GetProductByIdAsync(model.ProductId);

            if (product == null || product.Deleted)
            {
                errors.Add(await _localizationService.GetResourceAsync("Shipping.EstimateShippingPopUp.Product.IsNotFound"));
                return(Json(new
                {
                    Success = false,
                    Errors = errors
                }));
            }

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

            var addToCartWarnings = new List <string>();

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

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

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

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

            var result = await _shoppingCartModelFactory.PrepareEstimateShippingResultModelAsync(new[] { wrappedProduct }, model, false);

            return(Json(result));
        }