コード例 #1
0
        CartActionResponse UpdateCartFromViewModel(MinicartViewModel cartViewModel, Customer customer, CartTypeEnum cartType)
        {
            if (cartViewModel.CartItems == null)
            {
                return(null);
            }

            var messages = new List <CartActionMessage>();
            CartActionResponse response = null;

            foreach (var cartItem in cartViewModel.CartItems)
            {
                // Is this a Recurring Interval (Variant) change only?
                if (cartItem.RecurringVariantId != 0 &&
                    cartItem.VariantId != cartItem.RecurringVariantId &&
                    AppLogic.AppConfigBool("AllowRecurringFrequencyChangeInCart"))                        // Update Recurring Interval (Variant)
                {
                    response = CartActionProvider.ReplaceRecurringIntervalVariantInCart(
                        customer: customer,
                        cartType: cartType,
                        shoppingCartRecId: cartItem.Id,
                        oldVariantId: cartItem.VariantId,
                        newVariantId: cartItem.RecurringVariantId);
                }
                else                 // Perform regular Qty Update
                {
                    response = CartActionProvider.UpdateItemQuantityInCart(new UpdateQuantityContext()
                    {
                        Customer          = customer,
                        CartType          = cartType,
                        ShoppingCartRecId = cartItem.Id,
                        Quantity          = cartItem.Quantity
                    });
                }

                if (response.Status != CartActionStatus.Success)
                {
                    return(response);
                }

                messages.AddRange(response.Messages.ToList());
            }

            return(new CartActionResponse(
                       updatedCart: response.UpdatedCart
                       ?? CachedShoppingCartProvider.Get(customer, cartType, AppLogic.StoreID()),
                       status: CartActionStatus.Success,
                       messages: messages));
        }
コード例 #2
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);
        }
コード例 #3
0
        public ActionResult UpdateMinicart([Bind(Include = "CartItems")] MinicartViewModel cartViewModel, CartTypeEnum cartType, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(ActionNames.Index, ControllerNames.Minicart));
            }

            var customer = HttpContext.GetCustomer();

            if (!customer.HasCustomerRecord)
            {
                // Customer session has been lost somewhere - give them a generic error and redirect
                // This can happen if their session times out, or if they are using their browser's 'Back' button to get to an outdated minicart state
                NoticeProvider.PushNotice(AppLogic.GetString("minicart.updateerror"), NoticeType.Failure);
                return(Redirect(Url.MakeSafeReturnUrl(returnUrl)));
            }

            // Make request, check status
            var response = UpdateCartFromViewModel(cartViewModel, customer, cartType);

            switch (response.Status)
            {
            case CartActionStatus.Forbidden:
            case CartActionStatus.RequiresLogin:
            case CartActionStatus.SessionTimeout:
                foreach (var message in response.Messages)
                {
                    NoticeProvider.PushNotice(message.ConvertToNotice());
                }
                return(Json(
                           data: new AjaxAddToCartData(
                               status: response.Status.ToString(),
                               messages: null,
                               minicartData: GetMinicartData(customer, response.UpdatedCart, null)),
                           behavior: JsonRequestBehavior.AllowGet));

            default:
                break;
            }

            // Handle non-ajax calls
            if (!Request.IsAjaxRequest())
            {
                foreach (var message in response.Messages)
                {
                    NoticeProvider.PushNotice(message.ConvertToNotice());
                }

                return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout,
                                        new { returnUrl = Url.MakeSafeReturnUrl(returnUrl, Url.GetDefaultContinueShoppingUrl()) }));
            }

            // The view should reflect the real cart not the posted values in the modelstate.
            ModelState.Clear();
            return(Json(
                       GetMinicartData(
                           customer: customer,
                           cart: response.UpdatedCart,
                           messages: response.Messages.Select(
                               n => n.ConvertToAjaxNotice())),
                       JsonRequestBehavior.AllowGet));
        }