Ejemplo n.º 1
0
        KitAddToCartViewModel BuildDefaultKitAddToCartViewModel(
            KitProductData kitData,
            Product product,
            ProductVariant variant,
            int quantity,
            Customer customer,
            int?cartRecordId)
        {
            var cartType = cartRecordId == null
                                ? CartTypeEnum.ShoppingCart // we won't use this, so what it's set to doesn't matter
                                : CachedShoppingCartProvider.Get(customer, CartTypeEnum.ShoppingCart, AppLogic.StoreID())
                           .CartItems
                           .Where(c => c.ShoppingCartRecordID == cartRecordId)
                           .Any()
                                ? CartTypeEnum.ShoppingCart
                                : CartTypeEnum.WishCart;

            var isUnorderable = false;

            if (!AppLogic.AppConfigBool("KitInventory.AllowSaleOfOutOfStock") &&
                (!kitData.HasStock || kitData.HasRequiredOrReadOnlyButUnOrderableKitGroup))
            {
                isUnorderable = true;
            }

            var isCallToOrder = product.IsCalltoOrder;

            kitData.ComputePrices(quantity);

            var kitGroups = new List <KitGroupViewModel>();

            foreach (var group in kitData.Groups)
            {
                var kitItems = new List <KitItemViewModel>();
                foreach (var item in group.Items)
                {
                    var priceDeltaDisplay = FormatCurrencyDisplay(item.PriceDelta, customer, variant.IsTaxable);
                    var showOutOfStock    = item.HasMappedVariant &&
                                            !item.VariantHasStock &&
                                            !AppLogic.AppConfigBool("KitInventory.AllowSaleOfOutOfStock") &&
                                            AppLogic.AppConfigBool("KitInventory.ShowOutOfStockMessage");

                    var kitItemRelativePriceDeltaDisplayText = KitItemRelativePriceDeltaDisplayText(item, customer.CurrencySetting, variant.IsTaxable, kitData);

                    kitItems.Add(new KitItemViewModel
                    {
                        Name        = item.Name,
                        NameDisplay = !string.IsNullOrEmpty(kitItemRelativePriceDeltaDisplayText)
                                                        ? string.Format("{0} [{1}]", item.Name, kitItemRelativePriceDeltaDisplayText)
                                                        : item.Name,
                        Description       = item.Description,
                        IsDefault         = item.IsDefault,
                        PriceDelta        = item.PriceDelta,
                        PriceDeltaDisplay = priceDeltaDisplay,
                        WeightDelta       = item.WeightDelta,
                        DisplayOrder      = item.DisplayOrder,
                        Id         = item.Id,
                        IsSelected = item.IsSelected,
                        TextOption = item.TextOption,
                        ImageUrl   = item.HasImage
                                                        ? item.ImagePath
                                                        : string.Empty,
                        OutOfStockMessage = showOutOfStock
                                                        ? "Out of stock"
                            : string.Empty
                    });
                }

                var selectedItemId = group.FirstSelectedItem != null
                                        ? group.FirstSelectedItem.Id
                                        : 0;

                var groupViewModel = new KitGroupViewModel
                {
                    Id          = group.Id,
                    Name        = group.Name,
                    Description = group.Description,
                    Summary     = group.Summary,
                    ImageUrl    = group.HasImage
                                                        ? group.ImagePath
                                                        : string.Empty,
                    KitGroupType   = group.SelectionControl,
                    Items          = kitItems,
                    SelectedItemId = selectedItemId,
                    IsRequired     = group.IsRequired,
                    IsReadOnly     = group.IsReadOnly
                };

                kitGroups.Add(groupViewModel);
            }

            var regularBasePrice   = kitData.Price;
            var basePrice          = kitData.BasePrice + kitData.ReadOnlyItemsSum;
            var customizedPrice    = kitData.CustomizedPrice;
            var customerLevelPrice = kitData.LevelPrice;

            PayPalAd payPalAd = null;

            // If this is a single variant product, setup the PayPal ad.
            if (AppLogic.GetNextVariant(product.ProductID, variant.VariantID) == variant.VariantID)
            {
                payPalAd = new PayPalAd(PayPalAd.TargetPage.Product);
            }

            // Values need for the Schema.Org tags
            var storeDefaultCultureInfo = CultureInfo.GetCultureInfo(Localization.GetDefaultLocale());
            var formattedSchemaPrice    = string.Format(storeDefaultCultureInfo, "{0:C}", kitData.BasePrice);
            var schemaRegionInfo        = new RegionInfo(storeDefaultCultureInfo.Name);

            // Build the view model
            return(new KitAddToCartViewModel(
                       isUnorderable: isUnorderable,
                       isCallToOrder: isCallToOrder,
                       regularBasePrice: FormatCurrencyDisplay(regularBasePrice, customer, variant.IsTaxable),
                       basePrice: FormatCurrencyDisplay(basePrice, customer, variant.IsTaxable),
                       customizedPrice: FormatCurrencyDisplay(customizedPrice, customer, variant.IsTaxable),
                       customerLevelPrice: FormatCurrencyDisplay(customerLevelPrice, customer, variant.IsTaxable),
                       showRegularBasePrice: kitData.IsDiscounted,
                       showBasePrice: !AppLogic.AppConfigBool("HideKitPrice"),
                       showCustomerLevelPrice: kitData.HasCustomerLevelPricing,
                       showQuantity: AppLogic.AppConfigBool("ShowQuantityOnProductPage") &&
                       !AppLogic.AppConfigBool("HideKitQuantity"),
                       showBuyButton: AppLogic.AppConfigBool("ShowBuyButtons") &&
                       product.ShowBuyButton &&
                       !AppLogic.HideForWholesaleSite(customer.CustomerLevelID),
                       showWishlistButton: AppLogic.AppConfigBool("ShowWishButtons"),
                       hidePriceUntilCart: product.HidePriceUntilCart,
                       restrictedQuantities: RestrictedQuantityProvider.BuildRestrictedQuantityList(variant.RestrictedQuantities),
                       payPalAd: payPalAd,
                       showSchemaOrgPrice: kitData.BasePrice > 0 && !AppLogic.AppConfigBool("HideKitPrice"),
                       schemaBasePrice: formattedSchemaPrice,
                       isoThreeLetterCurrency: schemaRegionInfo.ISOCurrencySymbol,
                       schemaProductUrl: string.Format("{0}://schema.org/Product", Request.Url.Scheme),
                       schemaOfferUrl: string.Format("{0}://schema.org/Offer", Request.Url.Scheme),
                       cartType: cartType)
            {
                ProductId = product.ProductID,
                VariantId = variant.VariantID,
                CartRecordId = cartRecordId,
                Quantity = quantity,
                KitGroups = kitGroups,

                UpsellProducts = null,
                IsWishlist = false,
                ReturnUrl = UrlHelper.MakeSafeReturnUrl(Request.RawUrl),
                TemporaryImageNameStub = Guid.NewGuid().ToString()
            });
        }
Ejemplo n.º 2
0
        public AddToCartViewModel BuildAddToCartViewModel(
            UrlHelper urlHelper,
            ProductVariant variant,
            Product product,
            CartItem cartItem,
            Customer customer,
            bool showWishlistButton,
            bool colorSelectorChangesImage,
            int defaultQuantity  = 1,
            string selectedSize  = "",
            string selectedColor = "")
        {
            // Build our options and prompts
            var customerEntersPricePrompt = XmlCommon.GetLocaleEntry(variant.CustomerEntersPricePrompt, customer.LocaleSetting, true);

            customerEntersPricePrompt = !string.IsNullOrEmpty(customerEntersPricePrompt)
                                        ? customerEntersPricePrompt
                                        : AppLogic.GetString("common.cs.23", customer.LocaleSetting);

            var textOptionPrompt = XmlCommon.GetLocaleEntry(product.TextOptionPrompt, customer.LocaleSetting, true);

            textOptionPrompt = !string.IsNullOrEmpty(textOptionPrompt)
                                        ? textOptionPrompt
                                        : AppLogic.GetString("common.cs.70", customer.LocaleSetting);

            var sizes            = XmlCommon.GetLocaleEntry(variant.Sizes, Localization.GetDefaultLocale(), true);
            var displaySizes     = XmlCommon.GetLocaleEntry(variant.Sizes, customer.LocaleSetting, true);
            var sizeOptionPrompt = XmlCommon.GetLocaleEntry(product.SizeOptionPrompt, customer.LocaleSetting, true);

            sizeOptionPrompt = !string.IsNullOrEmpty(sizeOptionPrompt)
                                ? sizeOptionPrompt
                                : AppLogic.GetString("AppConfig.SizeOptionPrompt", customer.LocaleSetting);

            var colors            = XmlCommon.GetLocaleEntry(variant.Colors, Localization.GetDefaultLocale(), true);
            var displayColors     = XmlCommon.GetLocaleEntry(variant.Colors, customer.LocaleSetting, true);
            var colorOptionPrompt = XmlCommon.GetLocaleEntry(product.ColorOptionPrompt, customer.LocaleSetting, true);

            colorOptionPrompt = !string.IsNullOrEmpty(colorOptionPrompt)
                                ? colorOptionPrompt
                                : AppLogic.GetString("AppConfig.ColorOptionPrompt", customer.LocaleSetting);

            var quantity = defaultQuantity;

            if (cartItem != null && cartItem.Quantity > 0)
            {
                quantity = cartItem.Quantity;
            }
            else if (variant.MinimumQuantity > 0)
            {
                quantity = variant.MinimumQuantity;
            }
            else if (!string.IsNullOrEmpty(AppLogic.AppConfig("DefaultAddToCartQuantity")))
            {
                quantity = AppLogic.AppConfigUSInt("DefaultAddToCartQuantity");
            }

            selectedSize = cartItem != null
                                        ? AppLogic.CleanSizeColorOption(cartItem.ChosenSize)
                                        : AppLogic.CleanSizeColorOption(selectedSize);;

            selectedColor = cartItem != null
                                        ? AppLogic.CleanSizeColorOption(cartItem.ChosenColor)
                                        : AppLogic.CleanSizeColorOption(selectedColor);

            // If this is a single variant product, setup the PayPal ad.
            PayPalAd payPalAd = null;

            if (AppLogic.GetNextVariant(product.ProductID, variant.VariantID) == variant.VariantID)
            {
                payPalAd = new PayPalAd(PayPalAd.TargetPage.Product);
            }

            var variantCount = DB.GetSqlN(
                sql: "SELECT COUNT(*) AS N FROM ProductVariant WHERE Deleted = 0 and published = 1 and ProductID = @productId",
                parameters: new SqlParameter("productId", product.ProductID));

            // Now build the model
            var model = new AddToCartViewModel(
                showQuantity: (!variant.CustomerEntersPrice &&
                               AppLogic.AppConfigBool("ShowQuantityOnProductPage") &&
                               !product.IsAKit) ||
                !AppLogic.AppConfigBool("HideKitQuantity") &&
                product.IsAKit,
                restrictedQuantities: RestrictedQuantityProvider.BuildRestrictedQuantityList(variant.RestrictedQuantities),
                customerEntersPrice: variant.CustomerEntersPrice,
                customerEntersPricePrompt: customerEntersPricePrompt,
                colorOptions: BuildOptionList(colors, displayColors, customer, product.TaxClassID, colorOptionPrompt),
                colorOptionPrompt: colorOptionPrompt,
                sizeOptions: BuildOptionList(sizes, displaySizes, customer, product.TaxClassID, sizeOptionPrompt),
                sizeOptionPrompt: sizeOptionPrompt,
                requiresTextOption: product.RequiresTextOption,
                showTextOption: product.RequiresTextOption ||
                !string.IsNullOrEmpty(XmlCommon.GetLocaleEntry(product.TextOptionPrompt, customer.LocaleSetting, true)),
                textOptionPrompt: textOptionPrompt,
                textOptionMaxLength: product.TextOptionMaxLength == 0
                                        ? 50
                                        : product.TextOptionMaxLength,
                isCallToOrder: product.IsCalltoOrder,
                showBuyButton: AppLogic.AppConfigBool("ShowBuyButtons") &&
                product.ShowBuyButton &&
                !AppLogic.HideForWholesaleSite(customer.CustomerLevelID),
                showWishlistButton: showWishlistButton && AppLogic.AppConfigBool("ShowWishButtons"),
                payPalAd: payPalAd,
                showBuySafeKicker: (!AppLogic.AppConfigBool("BuySafe.DisableAddToCartKicker") &&
                                    AppLogic.GlobalConfigBool("BuySafe.Enabled") &&
                                    AppLogic.GlobalConfig("BuySafe.Hash").Length != 0),
                buySafeKickerType: AppLogic.AppConfig("BuySafe.KickerType"),
                colorSelectorChangesImage: colorSelectorChangesImage,
                isSimpleProduct: !product.IsAKit &&
                variantCount == 1 &&
                string.IsNullOrEmpty(displaySizes) &&
                string.IsNullOrEmpty(displayColors) &&
                !product.RequiresTextOption &&
                !variant.CustomerEntersPrice,
                cartType: cartItem != null
                                        ? cartItem.CartType
                                        : CartTypeEnum.ShoppingCart) // if no current cart item, this won't be used, so it doesn't matter what it's set to
            {
                ProductId    = variant.ProductID,
                VariantId    = variant.VariantID,
                CartRecordId = cartItem != null
                                        ? cartItem.ShoppingCartRecordID
                                        : 0,
                Quantity             = quantity,
                CustomerEnteredPrice = (cartItem != null && variant.CustomerEntersPrice)
                                        ? cartItem.Price
                                        : 0.00M,
                TextOption = cartItem != null
                                        ? cartItem.TextOption
                                        : String.Empty,
                Color          = selectedColor,
                Size           = selectedSize,
                UpsellProducts = null,
                IsWishlist     = false,
                ReturnUrl      = urlHelper.MakeSafeReturnUrl(HttpContext.Current.Request.RawUrl)
            };

            return(model);
        }