/// <summary> /// Initializes a new instance of the <see cref="WishlistLineModel"/> class. /// </summary> /// <param name="wishListLine">The wish list line.</param> public WishlistLineModel(WishListLine wishListLine) { Assert.ArgumentNotNull(wishListLine, "wishListLine"); this.Id = wishListLine.ExternalId; this.Quantity = wishListLine.Quantity; if (wishListLine.Product != null) { this.ProductId = wishListLine.Product.ProductId; if (wishListLine.Product.Price != null) { this.UnitPrice = wishListLine.Product.Price.Amount; } //this.ProductName = productName ?? line.Product.ProductId; this.ProductName = wishListLine.Product.ProductId; } if (wishListLine.Total != null) { this.TotalPrice = wishListLine.Total.Amount.ToString("C", Context.Language.CultureInfo); } }
public ManagerResponse <UpdateWishListLinesResult, WishList> UpdateWishListLines(CommerceStorefront storefront, WishList wishList, List <CartLineUpdateArgument> wishListLineUpdateArguments) { Assert.ArgumentNotNull(wishList, nameof(wishList)); Assert.ArgumentNotNull(storefront, nameof(storefront)); List <WishListLine> lineList = new List <WishListLine>(); foreach (CartLineUpdateArgument lineUpdateArgument in wishListLineUpdateArguments) { CartLineUpdateArgument inputModel = lineUpdateArgument; Assert.ArgumentNotNullOrEmpty(inputModel.ExternalLineId, "inputModel.ExternalLineId"); int quantity = (int)inputModel.LineArguments.Quantity; WishListLine wishListLine = wishList.Lines.FirstOrDefault(l => l.ExternalId == inputModel.ExternalLineId); if (wishListLine != null) { wishListLine.Quantity = quantity; lineList.Add(wishListLine); } } UpdateWishListLinesResult wishListResult = _wishListServiceProvider.UpdateWishListLines(new UpdateWishListLinesRequest(wishList, lineList)); Helpers.LogSystemMessages(wishListResult.SystemMessages, wishListResult); UpdateWishListLinesResult serviceProviderResult = wishListResult; return(new ManagerResponse <UpdateWishListLinesResult, WishList>(serviceProviderResult, serviceProviderResult.WishList)); }
/// <summary> /// Initializes a new instance of the <see cref="UpdateLinesOnWishlistTest"/> class. /// </summary> public UpdateLinesOnWishlistTest() { this.visitorId = Guid.NewGuid(); this.oldLine = new WishListLine() { ExternalId = "10", Product = new CartProduct { ProductId = "100500", Price = new Price { Amount = 100 } }, Quantity = 12 }; this.lineToUpdate = new WishListLine() { ExternalId = "10", Product = new CartProduct { ProductId = "100500", Price = new Price { Amount = 100 } }, Quantity = 2 }; this.wishlist = new WishList { ExternalId = this.visitorId.ToString(), Lines = new ReadOnlyCollection <WishListLine>(new List <WishListLine> { this.oldLine }) }; this.updatedWishlist = new WishList { ExternalId = this.visitorId.ToString(), Lines = new ReadOnlyCollection <WishListLine>(new List <WishListLine> { this.lineToUpdate }) }; this.request = new UpdateWishListLinesRequest(this.wishlist, new[] { this.lineToUpdate }); this.result = new UpdateWishListLinesResult(); this.args = new ServicePipelineArgs(this.request, this.result); this.client = Substitute.For <IWishlistsServiceChannel>(); var clientFactory = Substitute.For <ServiceClientFactory>(); clientFactory.CreateClient <IWishlistsServiceChannel>(Arg.Any <string>(), Arg.Any <string>()).Returns(this.client); this.processor = new UpdateLinesOnWishlist { ClientFactory = clientFactory }; }
protected List <WishListLine> TranslateLines(Cart source, WishList destination) { List <WishListLine> resultWishlist = new List <WishListLine>(); if (source.Lines != null) { foreach (var lineItem in source.Lines) { var wishListLine = new WishListLine { ExternalId = lineItem.Id, Product = new CartProduct() }; if (lineItem.CartLineComponents != null && !string.IsNullOrEmpty(lineItem.ItemId)) { CartProductComponent productComponent = lineItem.CartLineComponents.OfType <CartProductComponent>().FirstOrDefault(); var product = new CommerceCartProduct(); if (productComponent != null) { string[] array = lineItem.ItemId.Split("|".ToCharArray()); product.ProductCatalog = array[0]; product.ProductId = array[1]; product.ProductName = string.IsNullOrEmpty(productComponent.ProductName) ? productComponent.DisplayName : productComponent.ProductName; product.SitecoreProductItemId = GetSitecoreItemId(array[1], array[2]); destination.SetPropertyValue("_product_Images", productComponent.Image == null || string.IsNullOrEmpty(productComponent.Image.SitecoreId) ? string.Empty : productComponent.Image.SitecoreId); product.SetPropertyValue("Image", productComponent.Image == null || string.IsNullOrEmpty(productComponent.Image.SitecoreId) ? string.Empty : productComponent.Image.SitecoreId); product.SetPropertyValue("Color", string.IsNullOrEmpty(productComponent.Color) ? null : productComponent.Color); product.SetPropertyValue("Size", string.IsNullOrEmpty(productComponent.Size) ? null : productComponent.Size); product.SetPropertyValue("Style", string.IsNullOrEmpty(productComponent.Style) ? null : productComponent.Style); //if (!string.IsNullOrEmpty(productComponent.ExternalId) && // ID.TryParse(productComponent.ExternalId, out var result)) //{ // product.SitecoreProductItemId = result.ToGuid(); //} ItemVariationSelectedComponent selectedComponent = lineItem.CartLineComponents.OfType <ItemVariationSelectedComponent>().FirstOrDefault(); if (selectedComponent != null) { product.ProductId = productComponent.Id + "|" + selectedComponent.VariationId; product.ProductVariantId = selectedComponent.VariationId; } } if (lineItem.UnitListPrice != null) { product.Price = new Price(lineItem.UnitListPrice.Amount, lineItem.UnitListPrice.CurrencyCode); } wishListLine.Product = product; wishListLine.Quantity = lineItem.Quantity; } resultWishlist.Add(wishListLine); } } return(resultWishlist); }
/// <summary> /// Processes the arguments. /// </summary> /// <param name="args">The pipeline arguments.</param> public override void Process(ServicePipelineArgs args) { Assert.ArgumentNotNull(args, "args"); var request = (RemoveWishListLinesRequest)args.Request; var result = (RemoveWishListLinesResult)args.Result; var removedLines = new List <WishListLine>(); using (var client = this.GetClient()) { var wishlistId = new Guid(request.WishList.ExternalId); foreach (string lineId in request.LineIds) { WishListLine line = request.WishList.Lines.FirstOrDefault(p => p.ExternalId == lineId); if (line != null) { client.RemoveProduct(wishlistId, line.Product.ProductId); removedLines.Add(line); } } ShoppingCartModel wishlistModel = client.GetWishlist(wishlistId); var wishlist = new WishList { ExternalId = request.WishList.ExternalId }; wishlist.MapWishlistFromModel(wishlistModel); result.WishList = wishlist; result.RemovedLines = new ReadOnlyCollection <WishListLine>(removedLines); } }
public static WishlistLineModel ToWishlistLineModel([NotNull] this WishListLine wishListLine, IProductService productService = null) { var wishlistLineModel = new WishlistLineModel(wishListLine) { Id = wishListLine.ExternalId, Quantity = wishListLine.Quantity, }; if (productService != null) { var product = productService.ReadProduct(wishListLine.Product.ProductId); wishlistLineModel.ProductName = product.Name; string image = productService.GetImage(product); if (string.IsNullOrEmpty(image)) { image = productService.GetImages(product).FirstOrDefault(); wishlistLineModel.Image = image; } wishlistLineModel.Image = image; } return(wishlistLineModel); }
public void AddItem(CurrentUser user, int productID) { WishListLine line = new WishListLine { User = user.Data }; repository.SaveItem(line, productID); }
//protected override WishList GetTranslateDestination(TranslateCartToEntityRequest request) //{ // return this.EntityFactory.Create<WishList>("Cart"); //} protected virtual void TranslateLines(TranslateCartToEntityRequest request, Sitecore.Commerce.Plugin.Carts.Cart source, WishList destination) { Assert.ArgumentNotNull((object)request, nameof(request)); Assert.ArgumentNotNull((object)source, nameof(source)); Assert.ArgumentNotNull((object)destination, nameof(destination)); List <WishListLine> resultWishlist = new List <WishListLine>(); if (source.Lines != null) { foreach (var lineItem in source.Lines) { var wishListLine = new WishListLine() { ExternalId = lineItem.Id, Product = new CartProduct() }; if (lineItem.CartLineComponents != null && !string.IsNullOrEmpty(lineItem.ItemId)) { CartProductComponent productComponent = lineItem.CartLineComponents.OfType <CartProductComponent>().FirstOrDefault <CartProductComponent>(); var product = new CartProduct(); if (productComponent != null) { // string[] strArray = source.ItemId.Split("|".ToCharArray()); //product.ProductCatalog = strArray[0]; product.ProductId = productComponent.Id; product.ProductName = string.IsNullOrEmpty(productComponent.ProductName) ? productComponent.DisplayName : productComponent.ProductName; destination.SetPropertyValue("_product_Images", productComponent.Image == null || string.IsNullOrEmpty(productComponent.Image.SitecoreId) ? (object)string.Empty : (object)productComponent.Image.SitecoreId); product.SetPropertyValue("Color", string.IsNullOrEmpty(productComponent.Color) ? (object)(string)null : (object)productComponent.Color); product.SetPropertyValue("Size", string.IsNullOrEmpty(productComponent.Size) ? (object)(string)null : (object)productComponent.Size); product.SetPropertyValue("Style", string.IsNullOrEmpty(productComponent.Style) ? (object)(string)null : (object)productComponent.Style); ID result; if (!string.IsNullOrEmpty(productComponent.ExternalId) && ID.TryParse(productComponent.ExternalId, out result)) { product.SitecoreProductItemId = result.ToGuid(); } } ItemVariationSelectedComponent selectedComponent = lineItem.CartLineComponents.OfType <ItemVariationSelectedComponent>().FirstOrDefault <ItemVariationSelectedComponent>(); if (selectedComponent != null) { product.ProductId = productComponent.Id + "|" + selectedComponent.VariationId; } if (lineItem.UnitListPrice != null) { product.Price = new Price(lineItem.UnitListPrice.Amount, lineItem.UnitListPrice.CurrencyCode); } wishListLine.Product = product; wishListLine.Quantity = lineItem.Quantity; } resultWishlist.Add(wishListLine); } } }
public void SaveItem(WishListLine item, int itemID) { var product = context.Products.Find(itemID); item.Product = product; var user = item.User; user.WishList.Add(item); context.WishListLines.Add(item); context.SaveChanges(); }
public async Task <IActionResult> Create([Bind("WistListId,ProductId,Quantity")] WishListLine wishListLine) { if (ModelState.IsValid) { _context.Add(wishListLine); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "Genre", wishListLine.ProductId); ViewData["WistListId"] = new SelectList(_context.WishLists, "CustomerId", "CustomerId", wishListLine.WistListId); return(View(wishListLine)); }
public virtual void Initialize(WishListLine listLine) { this.ExternalWishListLineId = listLine.ExternalId; this.ProductId = listLine.Product.ProductId; this.Quantity = listLine.Quantity.ToString((IFormatProvider)Context.Language.CultureInfo); this.LinePrice = listLine.Product.Price.Amount.ToCurrency(); this.LineTotal = this.LinePrice; this.DisplayName = listLine.Product.ProductName; var prodId = this.ProductId.Split('|')[0]; var imageId = listLine.Product.GetPropertyValue("Image").ToString(); this.SetImageUrl(imageId); this.SetLink(prodId); }
public IResult AddToWishList(WishList wishList, Product product) { WishListLine wishListLine = wishList.WishListLines.FirstOrDefault(c => c.Product.Id == product.Id); if (wishListLine != null) { wishListLine.Quantity++; return(new SuccessResult()); } wishList.WishListLines.Add(new WishListLine { Product = product, Quantity = 1 }); return(new SuccessResult()); }
/// <summary> /// Initializes a new instance of the <see cref="WishListItemBaseJsonResult" /> class. /// </summary> /// <param name="line">The line.</param> /// <param name="wishListId">The wish list identifier.</param> public WishListItemBaseJsonResult(WishListLine line, string wishListId) { Assert.ArgumentNotNull(line, "line"); Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId"); var product = (CommerceCartProduct)line.Product; var productItem = Sitecore.Reference.Storefront.SitecorePipelines.ProductItemResolver.ResolveCatalogItem(product.ProductId, product.ProductCatalog, true); var currencyCode = StorefrontManager.GetCustomerCurrency(); this.DisplayName = product.DisplayName; this.Color = product.Properties["Color"] as string; this.LineDiscount = ((CommerceTotal)line.Total).LineItemDiscountAmount.ToString(Sitecore.Context.Language.CultureInfo); this.Quantity = line.Quantity.ToString(Sitecore.Context.Language.CultureInfo); this.LineTotal = line.Total.Amount.ToCurrency(currencyCode); this.ExternalLineId = line.ExternalId; this.ProductId = product.ProductId; this.VariantId = product.ProductVariantId; this.ProductCatalog = product.ProductCatalog; this.WishListId = wishListId; this.ProductUrl = product.ProductId.Equals(StorefrontManager.CurrentStorefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase) ? StorefrontManager.StorefrontUri("/buygiftcard") : LinkManager.GetDynamicUrl(productItem); if (product.Price.Amount != 0M) { this.LinePrice = product.Price.Amount.ToCurrency(currencyCode); } var imageInfo = product.Properties["_product_Images"] as string; if (imageInfo != null) { var imageId = imageInfo.Split('|')[0]; MediaItem mediaItem = Sitecore.Context.Database.GetItem(imageId); this.Image = mediaItem.GetImageUrl(100, 100); } var giftCardAmount = line.GetPropertyValue("GiftCardAmount"); if (giftCardAmount != null) { decimal amount = System.Convert.ToDecimal(giftCardAmount, Sitecore.Context.Language.CultureInfo); this.GiftCardAmount = amount.ToCurrency(currencyCode); } }
/// <summary> /// Adds the lines to wish list. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="model">The model.</param> /// <returns> /// The manager response with the wish list as the result. /// </returns> public virtual ManagerResponse <AddLinesToWishListResult, WishList> AddLinesToWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, AddToWishListInputModel model) { Assert.ArgumentNotNull(storefront, "storefront"); Assert.ArgumentNotNull(visitorContext, "visitorContext"); Assert.ArgumentNotNull(model, "model"); var product = new CommerceCartProduct { ProductCatalog = model.ProductCatalog, ProductId = model.ProductId, ProductVariantId = model.VariantId }; var line = new WishListLine { Product = product, Quantity = model.Quantity == null ? 1 : (uint)model.Quantity }; if (line.Product.ProductId.Equals(storefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase)) { line.Properties.Add("GiftCardAmount", model.GiftCardAmount); } // create wish list if (model.WishListId == null && !string.IsNullOrEmpty(model.WishListName)) { var newList = this.CreateWishList(storefront, visitorContext, model.WishListName).Result; if (newList == null) { return(new ManagerResponse <AddLinesToWishListResult, WishList>(new AddLinesToWishListResult { Success = false }, null)); } model.WishListId = newList.ExternalId; } var result = this.AddLinesToWishList(storefront, visitorContext, model.WishListId, new List <WishListLine> { line }); return(new ManagerResponse <AddLinesToWishListResult, WishList>(result.ServiceProviderResult, result.ServiceProviderResult.WishList)); }
/// <summary> /// Initializes a new instance of the <see cref="WishListItemBaseJsonResult" /> class. /// </summary> /// <param name="line">The line.</param> /// <param name="wishListId">The wish list identifier.</param> public WishListItemBaseJsonResult(WishListLine line, string wishListId) { Assert.ArgumentNotNull(line, "line"); Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId"); var product = (CommerceCartProduct)line.Product; var productItem = Sitecore.Reference.Storefront.SitecorePipelines.ProductItemResolver.ResolveCatalogItem(product.ProductId, product.ProductCatalog, true); this.DisplayName = product.DisplayName; this.Color = product.Properties["Color"] as string; this.LineDiscount = ((CommerceTotal)line.Total).LineItemDiscountAmount.ToString(Sitecore.Context.Language.CultureInfo); this.Quantity = line.Quantity.ToString(Sitecore.Context.Language.CultureInfo); this.LineTotal = line.Total.Amount.ToCurrency(StorefrontConstants.Settings.DefaultCurrencyCode); this.ExternalLineId = line.ExternalId; this.ProductId = product.ProductId; this.VariantId = product.ProductVariantId; this.ProductCatalog = product.ProductCatalog; this.WishListId = wishListId; this.ProductUrl = product.ProductId.Equals(StorefrontManager.CurrentStorefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase) ? StorefrontManager.StorefrontUri("/buygiftcard") : LinkManager.GetDynamicUrl(productItem); if (product.Price.Amount != 0M) { this.LinePrice = product.Price.Amount.ToCurrency(StorefrontConstants.Settings.DefaultCurrencyCode); } var imageInfo = product.Properties["_product_Images"] as string; if (imageInfo != null) { var imageId = imageInfo.Split('|')[0]; MediaItem mediaItem = Sitecore.Context.Database.GetItem(imageId); this.Image = mediaItem.GetImageUrl(100, 100); } var giftCardAmount = line.GetPropertyValue("GiftCardAmount"); if (giftCardAmount != null) { decimal amount = System.Convert.ToDecimal(giftCardAmount, Sitecore.Context.Language.CultureInfo); this.GiftCardAmount = amount.ToCurrency(StorefrontConstants.Settings.DefaultCurrencyCode); } }
/// <summary> /// Updates a single wish list line. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="model">The model.</param> /// <returns>The manager response with the updated wish list as the result.</returns> public virtual ManagerResponse <UpdateWishListLinesResult, WishList> UpdateWishListLine([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] WishListLineInputModel model) { Assert.ArgumentNotNull(storefront, "storefront"); Assert.ArgumentNotNull(visitorContext, "visitorContext"); Assert.ArgumentNotNull(model, "model"); Assert.ArgumentNotNullOrEmpty(model.WishListId, "model.WishListId"); Assert.ArgumentNotNullOrEmpty(model.ProductId, "model.ProductId"); var wishListLine = new WishListLine { Product = new CommerceCartProduct { ProductId = model.ProductId, ProductVariantId = model.VariantId }, Quantity = model.Quantity }; return(this.UpdateWishListLines(storefront, visitorContext, model.WishListId, new List <WishListLine> { wishListLine })); }
public virtual WishListJsonResult AddWishListLine(IStorefrontContext storefrontContext, IVisitorContext visitorContext, string catalogName, string productId, string variantId, Decimal quantity) { Assert.ArgumentNotNull((object)storefrontContext, nameof(storefrontContext)); Assert.ArgumentNotNull((object)visitorContext, nameof(visitorContext)); WishListJsonResult model = this.ModelProvider.GetModel <WishListJsonResult>(); CommerceStorefront currentStorefront = storefrontContext.CurrentStorefront; ManagerResponse <GetWishListResult, WishList> currentWishList = this.WishListManager.GetWishList(visitorContext, storefrontContext); if (!currentWishList.ServiceProviderResult.Success || currentWishList.Result == null) { string systemMessage = "Wish List not found."; currentWishList.ServiceProviderResult.SystemMessages.Add(new SystemMessage() { Message = systemMessage }); model.SetErrors((ServiceProviderResult)currentWishList.ServiceProviderResult); return(model); } List <WishListLine> wishListLines = new List <WishListLine>(); var wishlistLine = new WishListLine() { Quantity = quantity, Product = new Commerce.Entities.Carts.CartProduct() { ProductId = catalogName + "|" + productId + "|" + variantId, } }; wishListLines.Add(wishlistLine); ManagerResponse <AddLinesToWishListResult, WishList> managerResponse = this.WishListManager.AddLinesToWishList(currentStorefront, visitorContext, currentWishList.Result, wishListLines); if (!managerResponse.ServiceProviderResult.Success) { model.SetErrors((ServiceProviderResult)managerResponse.ServiceProviderResult); return(model); } model.Initialize(managerResponse.Result); model.Success = true; return(model); }
public async Task <IActionResult> AddToWishlist(int id) { string email = User.Identity.Name; var customers = _context.Customers .Where(c => c.Email == email) .Select(id => id.CustomerId) .ToList(); int customerId = customers.First(); var product = _context.Products.Find(id); var wishlist = _context.WishLists.Find(customerId); var line = _context.WishListLines.Find(customerId, id); if (line != null) { line.Quantity++; _context.Update(line); } else { line = new WishListLine() { ProductId = id, WistListId = customerId, Product = product, WistList = wishlist, Quantity = 1 }; _context.Add(line); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", "WishListLines")); }
/// <summary> /// Adds the lines to wish list. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="model">The model.</param> /// <returns> /// The manager response with the wish list as the result. /// </returns> public virtual ManagerResponse<AddLinesToWishListResult, WishList> AddLinesToWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, AddToWishListInputModel model) { Assert.ArgumentNotNull(storefront, "storefront"); Assert.ArgumentNotNull(visitorContext, "visitorContext"); Assert.ArgumentNotNull(model, "model"); var product = new CommerceCartProduct { ProductCatalog = model.ProductCatalog, ProductId = model.ProductId, ProductVariantId = model.VariantId }; var line = new WishListLine { Product = product, Quantity = model.Quantity == null ? 1 : (uint)model.Quantity }; if (line.Product.ProductId.Equals(storefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase)) { line.Properties.Add("GiftCardAmount", model.GiftCardAmount); } // create wish list if (model.WishListId == null && !string.IsNullOrEmpty(model.WishListName)) { var newList = this.CreateWishList(storefront, visitorContext, model.WishListName).Result; if (newList == null) { return new ManagerResponse<AddLinesToWishListResult, WishList>(new AddLinesToWishListResult { Success = false }, null); } model.WishListId = newList.ExternalId; } var result = this.AddLinesToWishList(storefront, visitorContext, model.WishListId, new List<WishListLine> { line }); return new ManagerResponse<AddLinesToWishListResult, WishList>(result.ServiceProviderResult, result.ServiceProviderResult.WishList); }
/// <summary> /// Updates a single wish list line. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="model">The model.</param> /// <returns>The manager response with the updated wish list as the result.</returns> public virtual ManagerResponse<UpdateWishListLinesResult, WishList> UpdateWishListLine([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] WishListLineInputModel model) { Assert.ArgumentNotNull(storefront, "storefront"); Assert.ArgumentNotNull(visitorContext, "visitorContext"); Assert.ArgumentNotNull(model, "model"); Assert.ArgumentNotNullOrEmpty(model.WishListId, "model.WishListId"); Assert.ArgumentNotNullOrEmpty(model.ProductId, "model.ProductId"); var wishListLine = new WishListLine { Product = new CommerceCartProduct { ProductId = model.ProductId, ProductVariantId = model.VariantId }, Quantity = model.Quantity }; return this.UpdateWishListLines(storefront, visitorContext, model.WishListId, new List<WishListLine> { wishListLine }); }