public virtual IEnumerable <ShipmentViewModel> CreateShipmentsViewModel(ICart cart)
        {
            var preferredCulture = _languageResolver.GetPreferredCulture();

            foreach (var shipment in cart.GetFirstForm().Shipments)
            {
                var shipmentModel = new ShipmentViewModel
                {
                    ShipmentId      = shipment.ShipmentId,
                    CartItems       = new List <CartItemViewModel>(),
                    Address         = _addressBookService.ConvertToModel(shipment.ShippingAddress),
                    ShippingMethods = CreateShippingMethodViewModels(cart.Market, cart.Currency, shipment)
                };

                shipmentModel.ShippingMethodId = shipment.ShippingMethodId == Guid.Empty && shipmentModel.ShippingMethods.Any() ?
                                                 shipmentModel.ShippingMethods.First().Id
                                               : shipment.ShippingMethodId;

                var variants = _contentLoader.GetItems(shipment.LineItems.Select(x => _referenceConverter.GetContentLink(x.Code)),
                                                       preferredCulture).OfType <VariationContent>();

                foreach (var lineItem in shipment.LineItems)
                {
                    var variant = variants.Single(x => x.Code == lineItem.Code);

                    var productLink = variant.GetParentProducts(_relationRepository).FirstOrDefault();
                    if (ContentReference.IsNullOrEmpty(productLink))
                    {
                        continue;
                    }

                    shipmentModel.CartItems.Add(_cartItemViewModelFactory.CreateCartItemViewModel(cart, lineItem, variant));
                }

                yield return(shipmentModel);
            }
        }
Esempio n. 2
0
        public virtual IEnumerable <ShipmentViewModel> CreateShipmentsViewModel(ICart cart)
        {
            var preferredCulture = _languageResolver.GetPreferredCulture();

            foreach (var shipment in cart.GetFirstForm().Shipments)
            {
                var shipmentModel = new ShipmentViewModel
                {
                    ShipmentId      = shipment.ShipmentId,
                    CartItems       = new List <CartItemViewModel>(),
                    Address         = _addressBookService.ConvertToModel(shipment.ShippingAddress),
                    ShippingMethods = CreateShippingMethodViewModels(cart.Market, cart.Currency, shipment)
                };

                shipmentModel.ShippingMethodId = shipment.ShippingMethodId == Guid.Empty && shipmentModel.ShippingMethods.Any() ?
                                                 shipmentModel.ShippingMethods.First().Id
                                               : shipment.ShippingMethodId;

                var entries = _contentLoader.GetItems(shipment.LineItems.Select(x => _referenceConverter.GetContentLink(x.Code)),
                                                      preferredCulture).OfType <EntryContentBase>();

                foreach (var lineItem in shipment.LineItems)
                {
                    var entry = entries.FirstOrDefault(x => x.Code == lineItem.Code);
                    if (entry == null)
                    {
                        //Entry was deleted, skip processing.
                        continue;
                    }

                    shipmentModel.CartItems.Add(_cartItemViewModelFactory.CreateCartItemViewModel(cart, lineItem, entry));
                }

                yield return(shipmentModel);
            }
        }