コード例 #1
0
        MinicartViewModel GetShoppingCartViewModel(ShoppingCart cart, Customer customer)
        {
            var viewCartItems       = new List <MinicartItemViewModel>();
            var showImages          = AppLogic.AppConfigBool("Minicart.ShowImages");
            var allowQuantityUpdate = AppLogic.AppConfigBool("Minicart.QuantityUpdate.Enabled");

            foreach (var cartItem in cart.CartItems)
            {
                var recurringVariants        = ProductVariant.GetVariants(cartItem.ProductID, false).Where(pc => pc.IsRecurring == true);
                var recurringIntervalOptions = new SelectList(recurringVariants, "VariantID", "LocaleName", cartItem.VariantID.ToString());

                var promotionText = cartItem.ThisShoppingCart.DiscountResults
                                    .Where(dr => dr.DiscountedItems
                                           .Where(di => di.ShoppingCartRecordId == cartItem.ShoppingCartRecordID)
                                           .Any())
                                    .Select(dr => dr.Promotion.UsageText)
                                    .FirstOrDefault();

                viewCartItems.Add(new MinicartItemViewModel
                {
                    Id                     = cartItem.ShoppingCartRecordID,
                    ProductId              = cartItem.ProductID,
                    VariantId              = cartItem.VariantID,
                    RecurringVariantId     = cartItem.VariantID,                    //Have to set this here for the dropdown's SelectedValue to be right
                    ProductName            = cartItem.ProductName,
                    VariantName            = cartItem.VariantName,
                    ChosenColor            = AppLogic.CleanSizeColorOption(cartItem.ChosenColor),
                    ChosenColorSkuModifier = cartItem.ChosenColorSKUModifier,
                    ChosenSize             = AppLogic.CleanSizeColorOption(cartItem.ChosenSize),
                    ChosenSizeSkuModifier  = cartItem.ChosenColorSKUModifier,
                    ProductSku             = cartItem.SKU,
                    Notes                  = cartItem.Notes,
                    Quantity               = cartItem.Quantity,
                    ProductUrl             = Url.BuildProductLink(
                        id: cartItem.ProductID,
                        seName: cartItem.SEName),
                    ProductImageUrl           = cartItem.ProductPicURL,
                    ProductImageAlternateText = cartItem.SEAltText,
                    ShowImage = showImages,
                    RecurringIntervalOptions = recurringIntervalOptions,
                    // nal
                    //LinkToProduct = AppLogic.AppConfigBool("LinkToProductPageInCart"),
                    LinkToProduct         = true,
                    TextOption            = cartItem.TextOptionDisplayFormat,
                    TextOptionLabel       = cartItem.TextOptionPrompt,
                    LineItemPromotionText = promotionText,
                    SubTotalDisplay       = cartItem.RegularPriceRateDisplayFormat,
                    VatDisplay            = cartItem.VatRateDisplayFormat,
                    IsAKit     = cartItem.IsAKit,
                    KitDetails = cartItem.KitComposition.Compositions
                                 .Select(comp => new KitCartItemViewModel
                    {
                        Name       = comp.Name,
                        IsImage    = comp.ContentIsImage,
                        TextOption = comp.TextOption
                    })
                                 .ToList(),
                    AllowQuantityUpdate = allowQuantityUpdate,
                    ShowEditLink        = AppLogic.AppConfigBool("ShowEditButtonInCartForKitProducts") && cartItem.IsAKit
                                                ? true
                                                : AppLogic.AppConfigBool("ShowEditButtonInCartForRegularProducts") && !cartItem.IsAKit
                                                        ? true
                                                        : false,
                    EditUrl = string.Format(
                        "{0}?cartRecordId={1}",
                        Url.BuildProductLink(
                            id: cartItem.ProductID,
                            seName: cartItem.SEName),
                        cartItem.ShoppingCartRecordID),
                    RestrictedQuantities = new SelectList(cartItem.RestrictedQuantities),
                    ShowSku = !cartItem.IsSystem && AppLogic.AppConfigBool("Minicart.ShowSku")
                });
            }

            var maximumCartItemsToDisplay = AppLogic.AppConfigNativeInt("MaximumNumberOfCartItemsToDisplay");

            var orderTotal = cart.Total(true);
            var subtotal   = cart.SubTotal(
                includeDiscount: false,
                onlyIncludeTaxableItems: false,
                includeDownloadItems: true,
                includeFreeShippingItems: true,
                includeSystemItems: true,
                useCustomerCurrencySetting: true);

            var taxTotal      = cart.TaxTotal();
            var shippingTotal = cart.ShippingTotal(
                includeDiscount: true,
                includeTax: true);
            var displayTotal  = orderTotal - taxTotal - shippingTotal;
            var discountTotal = Math.Round(displayTotal - subtotal, 2);

            var cartViewModel = new MinicartViewModel
            {
                CartItems = viewCartItems,
                MaximumCartItemsToDisplay = maximumCartItemsToDisplay,
                UseMaximumCartItemBehavor = maximumCartItemsToDisplay > 0 && viewCartItems.Count() > maximumCartItemsToDisplay,
                SubTotal = Localization.CurrencyStringForDisplayWithExchangeRate(subtotal, customer.CurrencySetting),
                Total    = Localization.CurrencyStringForDisplayWithExchangeRate(displayTotal, customer.CurrencySetting),
                Discount = discountTotal != 0
                                        ? Localization.CurrencyStringForDisplayWithExchangeRate(
                    discountTotal,
                    customer.CurrencySetting)
                                        : null,
                ItemCount      = viewCartItems.Sum(item => item.Quantity),
                OtherMiniCount = cart.CartType == CartTypeEnum.ShoppingCart
                                        ? CachedShoppingCartProvider.Get(customer, CartTypeEnum.WishCart, AppLogic.StoreID()).CartItems.Count()
                                        : CachedShoppingCartProvider.Get(customer, CartTypeEnum.ShoppingCart, AppLogic.StoreID()).CartItems.Count(),
                AllowQuantityUpdate = allowQuantityUpdate,
                MinicartEnabled     = AppLogic.AppConfigBool("Minicart.Enabled") && AppLogic.GetCurrentPageType() != PageTypes.Checkout,
                MiniwishEnabled     = AppLogic.AppConfigBool("ShowWishButtons") && AppLogic.GetCurrentPageType() != PageTypes.Checkout
            };

            return(cartViewModel);
        }
コード例 #2
0
        ShoppingCartViewModel GetShoppingCartViewModel(ShoppingCart cart, Customer customer)
        {
            var viewCartItems = new List <ShoppingCartItemViewModel>();

            foreach (var cartItem in cart.CartItems)
            {
                var recurringVariants = ProductVariant.GetVariants(cartItem.ProductID, false)
                                        .Where(pc => pc.IsRecurring &&
                                               pc.Published &&
                                               !pc.Deleted);
                var recurringIntervalOptions = new SelectList(recurringVariants, "VariantID", "LocaleName", cartItem.VariantID.ToString());

                var promotionText = cartItem.ThisShoppingCart.DiscountResults
                                    .Where(dr => dr.DiscountedItems
                                           .Where(di => di.ShoppingCartRecordId == cartItem.ShoppingCartRecordID)
                                           .Any())
                                    .Select(dr => dr.Promotion.UsageText)
                                    .FirstOrDefault();

                viewCartItems.Add(new ShoppingCartItemViewModel
                {
                    Id                     = cartItem.ShoppingCartRecordID,
                    ProductId              = cartItem.ProductID,
                    VariantId              = cartItem.VariantID,
                    RecurringVariantId     = cartItem.VariantID,                    //Have to set this here for the dropdown's SelectedValue to be right
                    ProductName            = cartItem.ProductName,
                    VariantName            = cartItem.VariantName,
                    ChosenColor            = AppLogic.CleanSizeColorOption(cartItem.ChosenColor),
                    ChosenColorSkuModifier = cartItem.ChosenColorSKUModifier,
                    ChosenSize             = AppLogic.CleanSizeColorOption(cartItem.ChosenSize),
                    ChosenSizeSkuModifier  = cartItem.ChosenColorSKUModifier,
                    ProductSku             = cartItem.SKU,
                    Price                  = cartItem.Price,
                    PriceDisplay           = cartItem.RegularPriceRateDisplayFormat,
                    Notes                  = cartItem.Notes,
                    Quantity               = cartItem.Quantity,
                    ProductUrl             = Url.BuildProductLink(
                        id: cartItem.ProductID,
                        seName: cartItem.SEName),
                    ProductImageUrl           = cartItem.ProductPicURL,
                    ProductImageAlternateText = !string.IsNullOrEmpty(cartItem.SEAltText)
                                                ? cartItem.SEAltText
                                                : cartItem.ProductName,
                    ShowCartImages           = AppLogic.AppConfigBool("ShowPicsInCart"),
                    ShowRecurringDropDown    = recurringVariants.Any() && AppLogic.AppConfigBool("AllowRecurringFrequencyChangeInCart"),
                    RecurringIntervalOptions = recurringIntervalOptions,
                    LinkToProduct            = AppLogic.AppConfigBool("LinkToProductPageInCart"),
                    TextOption            = cartItem.TextOptionDisplayFormat,
                    TextOptionLabel       = cartItem.TextOptionPrompt,
                    LineItemPromotionText = promotionText,
                    SubTotalDisplay       = cartItem.RegularPriceRateDisplayFormat,
                    VatDisplay            = cartItem.VatRateDisplayFormat,
                    IsAKit     = cartItem.IsAKit,
                    KitDetails = cartItem.KitComposition.Compositions
                                 .Select(comp => new KitCartItemViewModel
                    {
                        Name       = comp.Name,
                        IsImage    = comp.ContentIsImage,
                        TextOption = comp.TextOption
                    })
                                 .ToList(),
                    ShowEditLink =
                        (AppLogic.AppConfigBool("ShowEditButtonInCartForKitProducts") && cartItem.IsAKit) ||
                        (AppLogic.AppConfigBool("ShowEditButtonInCartForRegularProducts") && !cartItem.IsAKit),
                    EditUrl = string.Format(
                        "{0}?cartRecordId={1}",
                        Url.BuildProductLink(
                            id: cartItem.ProductID,
                            seName: cartItem.SEName),
                        cartItem.ShoppingCartRecordID),
                    RestrictedQuantities = new SelectList(cartItem.RestrictedQuantities),
                    ShowSku = !cartItem.IsSystem &&
                              cartItem.SKU.Length > 0
                });
            }

            var maximumCartItemsToDisplay = AppLogic.AppConfigNativeInt("MaximumNumberOfCartItemsToDisplay");

            var cartViewModel = new ShoppingCartViewModel
            {
                CartItems = viewCartItems,
                MaximumCartItemsToDisplay = maximumCartItemsToDisplay,
                UseMaximumCartItemBehavor = maximumCartItemsToDisplay > 0 && viewCartItems.Count() > maximumCartItemsToDisplay,
                SubTotal = Localization.CurrencyStringForDisplayWithExchangeRate(
                    cart.SubTotal(false, false, true, true, true, true),
                    customer.CurrencySetting)
            };

            return(cartViewModel);
        }