Exemplo n.º 1
0
        public virtual async Task <ICollection <ShippingMethod> > GetAvailableShippingMethodsAsync()
        {
            var availableShippingMethods = new List <ShippingMethod>();

            // TODO: Remake with shipmentId
            var serviceModels = await _cartApi.CartModuleGetShipmentMethodsAsync(_cart.Id);

            foreach (var serviceModel in serviceModels)
            {
                availableShippingMethods.Add(serviceModel.ToWebModel(_cart.Currency));
            }
            //Evaluate tax for shipping methods
            var taxEvalContext = _cart.ToTaxEvalContext();

            taxEvalContext.Lines.Clear();
            taxEvalContext.Lines.AddRange(availableShippingMethods.Select(x => x.ToTaxLine()));
            var taxResult = await _commerceApi.CommerceEvaluateTaxesAsync(_cart.StoreId, taxEvalContext);

            if (taxResult != null)
            {
                var taxRates = taxResult.Select(x => x.ToWebModel(_cart.Currency)).ToList();
                foreach (var shippingMethod in availableShippingMethods)
                {
                    shippingMethod.ApplyTaxRates(taxRates);
                }
            }
            //Evaluate promotions for shipping methods
            var promoEvalContext = _cart.ToPromotionEvaluationContext();
            await _promotionEvaluator.EvaluateDiscountsAsync(promoEvalContext, availableShippingMethods);

            return(availableShippingMethods);
        }
Exemplo n.º 2
0
        private async Task ValidateShipmentsAsync(ShoppingCart cart)
        {
            var workContext = _workContextFactory();

            foreach (var shipment in cart.Shipments.ToArray())
            {
                shipment.ValidationErrors.Clear();
                var availableShippingMethods = await _cartApi.CartModuleGetShipmentMethodsAsync(cart.Id);

                if (availableShippingMethods.Count == 0)
                {
                    shipment.ValidationWarnings.Add(new ShippingUnavailableError());
                    break;
                }
                if (!string.IsNullOrEmpty(shipment.ShipmentMethodCode))
                {
                    var existingShippingMethod = availableShippingMethods.Select(sm => sm.ToWebModel(cart.Currency)).FirstOrDefault(sm => shipment.HasSameMethod(sm));
                    if (existingShippingMethod == null)
                    {
                        shipment.ValidationWarnings.Add(new ShippingUnavailableError());
                        break;
                    }
                    if (existingShippingMethod != null)
                    {
                        if (existingShippingMethod.Price != shipment.ShippingPrice &&
                            (cart.ValidationType == ValidationType.PriceAndQuantity || cart.ValidationType == ValidationType.Price))
                        {
                            shipment.ValidationWarnings.Add(new ShippingPriceError(shipment.ShippingPrice));

                            cart.Shipments.Clear();
                            cart.Shipments.Add(existingShippingMethod.ToShipmentModel(cart.Currency));
                        }
                    }
                }
            }
        }