public ManagerResponse <CartResult, bool> AddLineItemsToCart(string userId, IEnumerable <AddCartLineInputModel> inputModelList) { Assert.ArgumentNotNull(inputModelList, nameof(inputModelList)); var cartResult = LoadCartByName(CommerceConstants.CartSettings.DefaultCartName, userId, false); if (!cartResult.Success || cartResult.Cart == null) { var message = DictionaryPhraseRepository.Current.Get("/System Messages/Cart/Cart Not Found Error", "Could not retrieve the cart for the current user"); cartResult.SystemMessages.Add(new SystemMessage { Message = message }); return(new ManagerResponse <CartResult, bool>(cartResult, cartResult.Success)); } var lines = new List <CartLine>(); foreach (var inputModel in inputModelList) { Assert.ArgumentNotNullOrEmpty(inputModel.ProductId, nameof(inputModel.ProductId)); Assert.ArgumentNotNullOrEmpty(inputModel.CatalogName, nameof(inputModel.CatalogName)); Assert.ArgumentNotNull(inputModel.Quantity, nameof(inputModel.Quantity)); if (inputModel.Quantity == null) { continue; } var quantity = (uint)inputModel.Quantity; var cartLine = new CommerceCartLine(inputModel.CatalogName, inputModel.ProductId, inputModel.VariantId == "-1" ? null : inputModel.VariantId, quantity) { Properties = { ["ProductUrl"] = inputModel.ProductUrl, ["ImageUrl"] = inputModel.ImageUrl } }; //UpdateStockInformation(cartLine, inputModel.CatalogName); lines.Add(cartLine); } CartCacheHelper.InvalidateCartCache(userId); var cart = cartResult.Cart as CommerceCart; var addLinesRequest = new AddCartLinesRequest(cart, lines); RefreshCart(addLinesRequest, true); var addLinesResult = CartServiceProvider.AddCartLines(addLinesRequest); if (addLinesResult.Success && addLinesResult.Cart != null) { CartCacheHelper.AddCartToCache(addLinesResult.Cart as CommerceCart); } AddBasketErrorsToResult(addLinesResult.Cart as CommerceCart, addLinesResult); addLinesResult.WriteToSitecoreLog(); return(new ManagerResponse <CartResult, bool>(addLinesResult, addLinesResult.Success)); }
public ManagerResponse <CartResult, Cart> AddLineItemsToCart(Cart cart, IEnumerable <CartLineArgument> cartLines, string giftCardProductId, string giftCardPageLink) { Assert.ArgumentNotNull(cart, nameof(cart)); Assert.ArgumentNotNull(cartLines, nameof(cartLines)); var cartLineList = new List <CartLine>(); foreach (CartLineArgument cartLine in cartLines) { Assert.ArgumentNotNullOrEmpty(cartLine.ProductId, "inputModel.ProductId"); Assert.ArgumentNotNullOrEmpty(cartLine.CatalogName, "inputModel.CatalogName"); Assert.ArgumentNotNull(cartLine.Quantity, "inputModel.Quantity"); decimal quantity = cartLine.Quantity; var commerceCartLine = new CommerceCartLine(cartLine.CatalogName, cartLine.ProductId, cartLine.VariantId == "-1" ? null : cartLine.VariantId, quantity); cartLineList.Add(commerceCartLine); } var request = new AddCartLinesRequest(cart, cartLineList); var cartResult = this.cartServiceProvider.AddCartLines(request); if (!cartResult.Success) { cartResult.SystemMessages.LogSystemMessages(cartResult); } return(new ManagerResponse <CartResult, Cart>(cartResult, cartResult.Cart)); }
/// <summary> /// Adds the line item to cart. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="inputModelList">The input model.</param> /// <returns> /// The manager response where the result is retuned indicating the success or failure of the operation. /// </returns> public virtual ManagerResponse <CartResult, bool> AddLineItemsToCart([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, IEnumerable <AddCartLineInputModel> inputModelList) { Assert.ArgumentNotNull(inputModelList, "inputModelList"); var cartResult = this.LoadCartByName(storefront.ShopName, storefront.DefaultCartName, visitorContext.UserId, false); if (!cartResult.Success || cartResult.Cart == null) { var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.CartNotFoundError); cartResult.SystemMessages.Add(new SystemMessage { Message = message }); return(new ManagerResponse <CartResult, bool>(cartResult, cartResult.Success)); } var lines = new List <CartLine>(); foreach (var inputModel in inputModelList) { Assert.ArgumentNotNullOrEmpty(inputModel.ProductId, "inputModel.ProductId"); Assert.ArgumentNotNullOrEmpty(inputModel.CatalogName, "inputModel.CatalogName"); Assert.ArgumentNotNull(inputModel.Quantity, "inputModel.Quantity"); var quantity = (uint)inputModel.Quantity; //// Special handling for a Gift Card if (inputModel.ProductId.Equals(storefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase)) { inputModel.VariantId = inputModel.GiftCardAmount.Value.ToString("000", CultureInfo.InvariantCulture); } var cartLine = new CommerceCartLine(inputModel.CatalogName, inputModel.ProductId, inputModel.VariantId == "-1" ? null : inputModel.VariantId, quantity); cartLine.Properties["ProductUrl"] = inputModel.ProductUrl; cartLine.Properties["ImageUrl"] = inputModel.ImageUrl; //// UpdateStockInformation(storefront, visitorContext, cartLine, inputModel.CatalogName); lines.Add(cartLine); } var cartCache = CommerceTypeLoader.CreateInstance <CartCacheHelper>(); cartCache.InvalidateCartCache(visitorContext.GetCustomerId()); var cart = cartResult.Cart as CommerceCart; var addLinesRequest = new AddCartLinesRequest(cart, lines); addLinesRequest.RefreshCart(true); var addLinesResult = this.CartServiceProvider.AddCartLines(addLinesRequest); if (addLinesResult.Success && addLinesResult.Cart != null) { cartCache.AddCartToCache(addLinesResult.Cart as CommerceCart); } this.AddBasketErrorsToResult(addLinesResult.Cart as CommerceCart, addLinesResult); Helpers.LogSystemMessages(addLinesResult.SystemMessages, addLinesResult); return(new ManagerResponse <CartResult, bool>(addLinesResult, addLinesResult.Success)); }
protected override void Translate(TranslateCartLineToEntityRequest request, CartLineComponent source, CommerceCartLine destination) { base.Translate(request, source, destination); destination.ExternalCartLineId = source.Id; destination.Quantity = source.Quantity; TranslateProduct(request, source, destination); TranslateAdjustments(request, source, destination); TranslateTotals(request, source, destination); TranslateBlockchainInfo(request, source, destination); }
private void UpdateStockInformation(CommerceCartLine cartLine, string catalogName) { Assert.ArgumentNotNull(cartLine, nameof(cartLine)); var products = new List <InventoryProduct> { new CommerceInventoryProduct { ProductId = cartLine.Product.ProductId, CatalogName = catalogName } }; var stockInfoResult = InventoryManager.GetStockInformation(products, StockDetailsLevel.Status).ServiceProviderResult; if (stockInfoResult.StockInformation == null || !stockInfoResult.StockInformation.Any()) { return; } var stockInfo = stockInfoResult.StockInformation.FirstOrDefault(); var orderableInfo = new OrderableInformation(); if (stockInfo != null && stockInfo.Status != null) { if (Equals(stockInfo.Status, StockStatus.PreOrderable)) { var preOrderableResult = InventoryManager.GetPreOrderableInformation(products).ServiceProviderResult; if (preOrderableResult.OrderableInformation != null && preOrderableResult.OrderableInformation.Any()) { orderableInfo = preOrderableResult.OrderableInformation.FirstOrDefault(); } } else if (Equals(stockInfo.Status, StockStatus.BackOrderable)) { var backOrderableResult = InventoryManager.GetBackOrderableInformation(products).ServiceProviderResult; if (backOrderableResult.OrderableInformation != null && backOrderableResult.OrderableInformation.Any()) { orderableInfo = backOrderableResult.OrderableInformation.FirstOrDefault(); } } } if (stockInfo != null) { cartLine.Product.StockStatus = stockInfo.Status; } if (orderableInfo == null) { return; } cartLine.Product.InStockDate = orderableInfo.InStockDate; cartLine.Product.ShippingDate = orderableInfo.ShippingDate; }
protected virtual void TranslateBlockchainInfo(TranslateCartLineToEntityRequest request, CartLineComponent source, CommerceCartLine destination) { var blokchainTokenComponent = source.CartLineComponents.FirstOrDefault(c => c is DigitalDownloadBlockchainTokenComponent) as DigitalDownloadBlockchainTokenComponent; if (blokchainTokenComponent == null) { return; } destination.SetPropertyValue(Constants.BlockchainDownloadToken, blokchainTokenComponent.BlockchainDownloadToken); }
public ActionResult AddProduct() { var loadCartRequest = new LoadCartRequest("CommerceEngineDefaultStorefront", "Default", "1234"); var loadCartResult = _cartServiceProvider.LoadCart(loadCartRequest); var cart = loadCartResult.Cart; // Add a cart line: note that adding a line for the same product twice will update the quantity, not add a line: this is configured in commerce engine (look for RollupCartLinesPolicy) var lines = new List <CartLine>(); var cartLine = new CommerceCartLine("Neu", "47838_aus_allen_sternen_liebe_cd", "", 1.0M); lines.Add(cartLine); var addLinesRequest = new AddCartLinesRequest(cart, lines); var addLinesResult = _cartServiceProvider.AddCartLines(addLinesRequest); return(View("Cart", addLinesResult)); }
/// <summary> /// Determines whether this instance [can line item be emailed] the specified line item. /// </summary> /// <param name="lineItem">The line item.</param> /// <returns>True if the line item can be emailed; otherwise false.</returns> protected virtual bool CanLineItemBeEmailed(CommerceCartLine lineItem) { bool lineItemCanBeEmailed = true; var product = (CommerceCartProduct)lineItem.Product; var repository = ContextTypeLoader.CreateInstance <ICatalogRepository>(); Product commerceProduct = repository.GetProduct(product.ProductCatalog, product.ProductId); if (commerceProduct != null) { if (!commerceProduct.DefinitionName.Equals("GiftCard", StringComparison.OrdinalIgnoreCase)) { lineItemCanBeEmailed = false; } } return(lineItemCanBeEmailed); }
public ActionResult GetCartLineFulfillmentMethods() { // Load a cart var loadCartRequest = new LoadCartRequest("CommerceEngineDefaultStorefront", "Default", "1234"); var loadCartResult = _cartServiceProvider.LoadCart(loadCartRequest); var cart = loadCartResult.Cart as CommerceCart; // Add a line to the cart var lines = new List <CartLine>(); var cartLine = new CommerceCartLine("NEU", "test", "", 1.0M); lines.Add(cartLine); var addLinesRequest = new AddCartLinesRequest(cart, lines); var addLinesResult = _cartServiceProvider.AddCartLines(addLinesRequest); cart = addLinesResult.Cart as CommerceCart; var shippingService = new ShippingServiceProvider(); var shippingOption = new ShippingOption { ShippingOptionType = ShippingOptionType .DeliverItemsIndividually, // This will trigger calling GetCartLinesFulfillmentMethods instead of GetCartFulfillmentMethods }; var shippingParty = new CommerceParty { Address1 = "Main Street", City = "Montreal", ZipPostalCode = "NW7 7SJ", Country = "Canada", CountryCode = "CA" }; var request = new GetShippingMethodsRequest(shippingOption, shippingParty, cart) { Lines = cart.Lines.Cast <CommerceCartLine>().ToList() }; var result = shippingService.GetShippingMethods(request); return(View(result)); }
protected override void TranslateProduct( TranslateCartLineToEntityRequest request, CartLineComponent source, CommerceCartLine destination, bool isSubLine = false) { base.TranslateProduct(request, source, destination, isSubLine); if (destination == null || destination.Product == null) { return; } if (source.CartLineComponents != null && !string.IsNullOrEmpty(source.ItemId)) { CartProductComponent productComponent = source.CartLineComponents.OfType <CartProductComponent>().FirstOrDefault(); if (productComponent != null) { destination.SetPropertyValue("ItemType", string.IsNullOrEmpty(productComponent.ItemType) ? null : productComponent.ItemType); } } }
/// <summary> /// Adds the line item to cart. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="inputModelList">The input model.</param> /// <returns> /// The manager response where the result is retuned indicating the success or failure of the operation. /// </returns> public virtual ManagerResponse<CartResult, bool> AddLineItemsToCart([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, IEnumerable<AddCartLineInputModel> inputModelList) { Assert.ArgumentNotNull(inputModelList, "inputModelList"); var cartResult = this.LoadCartByName(storefront.ShopName, storefront.DefaultCartName, visitorContext.UserId, false); if (!cartResult.Success || cartResult.Cart == null) { var message = StorefrontManager.GetSystemMessage("CartNotFoundError"); cartResult.SystemMessages.Add(new SystemMessage { Message = message }); return new ManagerResponse<CartResult, bool>(cartResult, cartResult.Success); } var lines = new List<CartLine>(); foreach (var inputModel in inputModelList) { Assert.ArgumentNotNullOrEmpty(inputModel.ProductId, "inputModel.ProductId"); Assert.ArgumentNotNullOrEmpty(inputModel.CatalogName, "inputModel.CatalogName"); Assert.ArgumentNotNull(inputModel.Quantity, "inputModel.Quantity"); var quantity = (uint)inputModel.Quantity; //// Special handling for a Gift Card if (inputModel.ProductId.Equals(storefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase)) { inputModel.VariantId = inputModel.GiftCardAmount.Value.ToString("000", CultureInfo.InvariantCulture); } var cartLine = new CommerceCartLine(inputModel.CatalogName, inputModel.ProductId, inputModel.VariantId == "-1" ? null : inputModel.VariantId, quantity); cartLine.Properties["ProductUrl"] = inputModel.ProductUrl; cartLine.Properties["ImageUrl"] = inputModel.ImageUrl; //// UpdateStockInformation(storefront, visitorContext, cartLine, inputModel.CatalogName); lines.Add(cartLine); } var cartCache = CommerceTypeLoader.CreateInstance<CartCacheHelper>(); cartCache.InvalidateCartCache(visitorContext.GetCustomerId()); var cart = cartResult.Cart as CommerceCart; var addLinesRequest = new AddCartLinesRequest(cart, lines); addLinesRequest.RefreshCart(true); var addLinesResult = this.CartServiceProvider.AddCartLines(addLinesRequest); if (addLinesResult.Success && addLinesResult.Cart != null) { cartCache.AddCartToCache(addLinesResult.Cart as CommerceCart); } this.AddBasketErrorsToResult(addLinesResult.Cart as CommerceCart, addLinesResult); Helpers.LogSystemMessages(addLinesResult.SystemMessages, addLinesResult); return new ManagerResponse<CartResult, bool>(addLinesResult, addLinesResult.Success); }
/// <summary> /// Determines whether this instance [can line item be emailed] the specified line item. /// </summary> /// <param name="lineItem">The line item.</param> /// <returns>True if the line item can be emailed; otherwise false.</returns> protected virtual bool CanLineItemBeEmailed(CommerceCartLine lineItem) { bool lineItemCanBeEmailed = true; var product = (CommerceCartProduct)lineItem.Product; var repository = CommerceTypeLoader.CreateInstance<ICatalogRepository>(); CSCatalog.Product commerceProduct = repository.GetProduct(product.ProductCatalog, product.ProductId); if (commerceProduct != null) { if (!commerceProduct.DefinitionName.Equals("GiftCard", StringComparison.OrdinalIgnoreCase)) { lineItemCanBeEmailed = false; } } return lineItemCanBeEmailed; }
public ActionResult BasketToOrder() { var shopName = "CommerceEngineDefaultStorefront"; // Better use a configured store, not CommerceEngineDefaultStorefront as it's not really configured var cartName = "Default"; var userId = "Entity-Customer-34d758ae2d2d472d89014954d0cc4440"; var domain = Context.User.Domain; // Get a cart var loadCartRequest = new LoadCartByNameRequest(shopName, cartName, userId); var cartResult = _cartServiceProvider.LoadCart(loadCartRequest); Assert.IsTrue(cartResult.Success, String.Join("|", cartResult.SystemMessages.Select(e => e.Message))); var cart = cartResult.Cart; var quantity = 0.0M; // Get initial quantity if (cart.Lines.Count > 0) { quantity = cart.Lines[0].Quantity; } // Add a cart line: note that adding a line for the same product twice will update the quantity, not add a line: this is configured in commerce engine (look for RollupCartLinesPolicy) var lines = new List <CartLine>(); var cartLine = new CommerceCartLine("Neu", "47838_aus_allen_sternen_liebe_cd", "", 1.0M); lines.Add(cartLine); var addLinesRequest = new AddCartLinesRequest(cart, lines); var addLinesResult = _cartServiceProvider.AddCartLines(addLinesRequest); Assert.IsTrue(addLinesResult.Success, String.Join("|", addLinesResult.SystemMessages.Select(e => e.Message))); var updatedCart = addLinesResult.Cart; Assert.IsTrue(updatedCart.Lines.Count > 0, "Updated cart should have at least one line"); Assert.IsTrue(quantity + 1 == updatedCart.Lines[0].Quantity, "new quantity should be one more than before"); var reloadCartRequest = new LoadCartByNameRequest(shopName, cartName, userId); var reloadedCartResult = _cartServiceProvider.LoadCart(reloadCartRequest); Assert.IsTrue(reloadedCartResult.Success, String.Join("|", reloadedCartResult.SystemMessages.Select(e => e.Message))); var reloadedCart = reloadedCartResult.Cart; Assert.IsTrue(reloadedCart.Lines.Count > 0, "Reloaded cart should have at least one line"); Assert.IsTrue(quantity + 1 == reloadedCart.Lines[0].Quantity, "reloaded cart quantity should be one more than before"); // Switching cart :-) cart = reloadedCart; // Add a shipping address CommerceParty shippingAddress = new CommerceParty(); shippingAddress.ExternalId = "Shipping"; shippingAddress.PartyId = shippingAddress.ExternalId; shippingAddress.Name = "Shipping"; shippingAddress.Address1 = "Barbara Strozzilaan 201"; shippingAddress.Company = "Sitecore"; shippingAddress.Country = "Canada"; shippingAddress.State = "ON"; // State is checked by commerce engine: you can configure it in Commerce shippingAddress.CountryCode = "CA"; // Country is checked by commerce engine shippingAddress.LastName = "Werkman"; shippingAddress.FirstName = "Erwin"; shippingAddress.City = "Amsterdam"; shippingAddress.ZipPostalCode = "1030AC"; var cartParties = cart.Parties.ToList(); cartParties.Add(shippingAddress); cart.Parties = cartParties; ShippingOptionType shippingOptionType = ShippingOptionType.ShipToAddress; ICollection <CommerceShippingInfo> shippingInfoList = new List <CommerceShippingInfo>(); var commerceShippingInfo = new CommerceShippingInfo(); commerceShippingInfo.ShippingOptionType = ShippingOptionType.ShipToAddress; commerceShippingInfo.PartyID = shippingAddress.ExternalId; commerceShippingInfo.ShippingMethodID = "B146622D-DC86-48A3-B72A-05EE8FFD187A"; // Ship Items > Ground commerceShippingInfo.ShippingMethodName = "Ground"; // Method id and name have to match what is configured in Sitecore Commerce Control Panel shippingInfoList.Add(commerceShippingInfo); var csShippingInfoList = new List <ShippingInfo>(); foreach (var shippingInfo in shippingInfoList) { csShippingInfoList.Add(shippingInfo); } // Add a shipping address and shipping method var addShippingInfoRequest = new Sitecore.Commerce.Engine.Connect.Services.Carts.AddShippingInfoRequest(cart, csShippingInfoList, shippingOptionType); var result = _cartServiceProvider.AddShippingInfo(addShippingInfoRequest); Assert.IsTrue(result.Success, String.Join("|", result.SystemMessages.Select(e => e.Message))); // Reload the cart so we have the latest information on how much we need to pay reloadedCartResult = _cartServiceProvider.LoadCart(reloadCartRequest); Assert.IsTrue(reloadedCartResult.Success, String.Join("|", reloadedCartResult.SystemMessages.Select(e => e.Message))); cart = reloadedCartResult.Cart; // Add billing address CommerceParty billingAddress = new CommerceParty(); billingAddress.ExternalId = "Billing"; // This should correspond to the PartyId you are setting for the payment info billingAddress.PartyId = billingAddress.ExternalId; billingAddress.Name = "Billing"; billingAddress.Address1 = "Dorpsstraat 50"; billingAddress.Company = "Sitecore"; billingAddress.Country = "Canada"; billingAddress.State = "ON"; // State is checked: you can configure it in Commerce billingAddress.CountryCode = "CA"; billingAddress.LastName = "Werkman"; billingAddress.FirstName = "Erwin"; billingAddress.City = "Amsterdam"; billingAddress.ZipPostalCode = "1234AK"; cart.Parties.Add(billingAddress); // Add a payment address and payment method var payments = new List <PaymentInfo>(); /* * For federated payment info to work, you need to set up the Braintree integration * var federatedPaymentInfo = new FederatedPaymentInfo(); * * federatedPaymentInfo.PartyID = billingAddress.ExternalId; * federatedPaymentInfo.PaymentMethodID = "0CFFAB11-2674-4A18-AB04-228B1F8A1DEC"; // Federated Payment * federatedPaymentInfo.Amount = cart.Total.Amount; // Total payment (of all payment methods for a cart) should always be the same as the total amount of the order * federatedPaymentInfo.CardToken = "payment-nonce"; // This should be valid b * * payments.Add(federatedPaymentInfo); */ var giftCardPaymentInfo = new GiftCardPaymentInfo(); giftCardPaymentInfo.PaymentMethodID = "B5E5464E-C851-4C3C-8086-A4A874DD2DB0"; // GiftCard giftCardPaymentInfo.Amount = cart.Total.Amount; giftCardPaymentInfo.ExternalId = "GC1000000"; // This is the number of the giftcard giftCardPaymentInfo.PartyID = billingAddress.ExternalId; payments.Add(giftCardPaymentInfo); var addPaymentInfoRequest = new AddPaymentInfoRequest(cart, payments); var addPaymentInfoResult = _cartServiceProvider.AddPaymentInfo(addPaymentInfoRequest); Assert.IsTrue(addPaymentInfoResult.Success, String.Join("|", addPaymentInfoResult.SystemMessages.Select(e => e.Message))); var freshCartRequest = new LoadCartByNameRequest(shopName, cartName, userId); var freshCartResult = _cartServiceProvider.LoadCart(reloadCartRequest); Assert.IsTrue(freshCartResult.Success, String.Join("|", freshCartResult.SystemMessages.Select(e => e.Message))); cart.Email = "*****@*****.**"; // This is necessary otherwise the cart will not become an order // Save the cart as an order var submitVisitorOrderRequest = new SubmitVisitorOrderRequest(cart); var submitVisitorOrderResult = _orderServiceProvider.SubmitVisitorOrder(submitVisitorOrderRequest); Assert.IsTrue(submitVisitorOrderResult.Success, String.Join("|", submitVisitorOrderResult.SystemMessages.Select(e => e.Message))); return(View("Cart", freshCartResult)); }
public ActionResult Index() { var loadCartRequest = new LoadCartRequest("CommerceEngineDefaultStorefront", "Default", "1234"); var loadCartResult = _cartServiceProvider.LoadCart(loadCartRequest); var cart = loadCartResult.Cart as CommerceCart; var lines = new List <CartLine>(); var cartLine = new CommerceCartLine("Habitat_Master", "6042567", "56042567", 1.0M); lines.Add(cartLine); var addLinesRequest = new AddCartLinesRequest(cart, lines); var addLinesResult = _cartServiceProvider.AddCartLines(addLinesRequest); // Add a shipping address CommerceParty shippingAddress = new CommerceParty(); shippingAddress.ExternalId = "Shipping"; shippingAddress.PartyId = shippingAddress.ExternalId; shippingAddress.Name = "Shipping"; shippingAddress.Address1 = "Barbara Strozzilaan 201"; shippingAddress.Company = "Sitecore"; shippingAddress.Country = "Canada"; shippingAddress.State = "ON"; // State is checked by commerce engine: you can configure it in Commerce shippingAddress.CountryCode = "CA"; // Country is checked by commerce engine shippingAddress.LastName = "Werkman"; shippingAddress.FirstName = "Erwin"; shippingAddress.City = "Amsterdam"; shippingAddress.ZipPostalCode = "1030AC"; var cartParties = cart.Parties.ToList(); cartParties.Add(shippingAddress); cart.Parties = cartParties; ShippingOptionType shippingOptionType = ShippingOptionType.ShipToAddress; ICollection <CommerceShippingInfo> shippingInfoList = new List <CommerceShippingInfo>(); var commerceShippingInfo = new CommerceShippingInfo(); commerceShippingInfo.ShippingOptionType = ShippingOptionType.ShipToAddress; commerceShippingInfo.PartyID = shippingAddress.ExternalId; commerceShippingInfo.ShippingMethodID = "B146622D-DC86-48A3-B72A-05EE8FFD187A"; // Ship Items > Ground commerceShippingInfo.ShippingMethodName = "Ground"; // Method id and name have to match what is configured in Sitecore Commerce Control Panel shippingInfoList.Add(commerceShippingInfo); var csShippingInfoList = new List <ShippingInfo>(); foreach (var shippingInfo in shippingInfoList) { csShippingInfoList.Add(shippingInfo); } // Add a shipping address and shipping method var addShippingInfoRequest = new Sitecore.Commerce.Engine.Connect.Services.Carts.AddShippingInfoRequest(cart, csShippingInfoList, shippingOptionType); var result = _cartServiceProvider.AddShippingInfo(addShippingInfoRequest); cart = result.Cart as CommerceCart; // Add billing address CommerceParty billingAddress = new CommerceParty(); billingAddress.ExternalId = "Billing"; // This should correspond to the PartyId you are setting for the payment info billingAddress.PartyId = billingAddress.ExternalId; billingAddress.Name = "Billing"; billingAddress.Address1 = "Dorpsstraat 50"; billingAddress.Company = "Sitecore"; billingAddress.Country = "Canada"; billingAddress.State = "ON"; // State is checked: you can configure it in Commerce billingAddress.CountryCode = "CA"; billingAddress.LastName = "Werkman"; billingAddress.FirstName = "Erwin"; billingAddress.City = "Amsterdam"; billingAddress.ZipPostalCode = "1234AK"; cart.Parties.Add(billingAddress); // Add a payment address and payment method var payments = new List <PaymentInfo>(); var simplePaymentInfo = new SimplePaymentInfo(); simplePaymentInfo.PaymentMethodID = "9B110CC3-C7C8-4492-8FCF-0CDE5D3E0EB0"; simplePaymentInfo.Amount = cart.Total.Amount; payments.Add(simplePaymentInfo); /* * var giftCardPaymentInfo = new GiftCardPaymentInfo(); * giftCardPaymentInfo.PaymentMethodID = "B5E5464E-C851-4C3C-8086-A4A874DD2DB0"; // GiftCard * giftCardPaymentInfo.Amount = cart.Total.Amount; * giftCardPaymentInfo.ExternalId = "GC1000000"; // This is the number of the giftcard * giftCardPaymentInfo.PartyID = billingAddress.ExternalId; // For a gift card this is not really necessary and not recorded * * payments.Add(giftCardPaymentInfo); */ var addPaymentInfoRequest = new AddPaymentInfoRequest(cart, payments); var addPaymentInfoResult = _cartServiceProvider.AddPaymentInfo(addPaymentInfoRequest); return(View("Cart", addPaymentInfoResult)); }
protected override void Translate(TranslateCartLineToEntityRequest request, CartLineComponent source, CommerceCartLine destination) { base.Translate(request, source, destination); var personalizationComponent = source.CartLineComponents.OfType <PersonalizationComponent>().FirstOrDefault(); if (personalizationComponent != null && !string.IsNullOrEmpty(personalizationComponent.PersonalizationId)) { destination.SetPropertyValue("PersonalizationId", personalizationComponent.PersonalizationId); } }
protected override void TranslateProduct(TranslateCartLineToEntityRequest request, CartLineComponent source, CommerceCartLine destination, bool isSubLine = false) { Assert.ArgumentNotNull((object)request, nameof(request)); Assert.ArgumentNotNull((object)source, nameof(source)); Assert.ArgumentNotNull((object)destination, nameof(destination)); CommerceCartProduct commerceCartProduct = this.EntityFactory.Create <CommerceCartProduct>("CartProduct"); if (source.CartLineComponents != null && !string.IsNullOrEmpty(source.ItemId)) { CartProductComponent productComponent = source.CartLineComponents.OfType <CartProductComponent>().FirstOrDefault <CartProductComponent>(); if (productComponent != null) { string[] strArray = source.ItemId.Split("|".ToCharArray()); commerceCartProduct.ProductCatalog = strArray[0]; commerceCartProduct.ProductId = productComponent.Id; commerceCartProduct.DisplayName = productComponent.DisplayName; commerceCartProduct.ProductName = string.IsNullOrEmpty(productComponent.ProductName) ? productComponent.DisplayName : productComponent.ProductName; commerceCartProduct.SitecoreProductItemId = this.GetSitecoreItemId(strArray[1], strArray[2]); destination.SetPropertyValue("_product_Images", productComponent.Image == null || string.IsNullOrEmpty(productComponent.Image.SitecoreId) ? (object)string.Empty : (object)productComponent.Image.SitecoreId); commerceCartProduct.SetPropertyValue("Color", string.IsNullOrEmpty(productComponent.Color) ? (object)(string)null : (object)productComponent.Color); commerceCartProduct.SetPropertyValue("Size", string.IsNullOrEmpty(productComponent.Size) ? (object)(string)null : (object)productComponent.Size); commerceCartProduct.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)) { commerceCartProduct.SitecoreProductItemId = result.ToGuid(); } } ItemVariationSelectedComponent selectedComponent = source.CartLineComponents.OfType <ItemVariationSelectedComponent>().FirstOrDefault <ItemVariationSelectedComponent>(); if (selectedComponent != null) { commerceCartProduct.ProductVariantId = selectedComponent.VariationId; } //Set an additional property to determine the Promotion Awarding Blocks [Promotion Plugin Issue#2 described in Known Issues with Promotion plugin blog post] if (source.Adjustments != null && source.Adjustments.Any()) { destination.SetPropertyValue(CartAdjustmentTypePropertyName, string.Join("|", source.Adjustments.Select(x => x.AwardingBlock))); } } CommercePrice commercePrice = this.EntityFactory.Create <CommercePrice>("Price"); if (source.UnitListPrice != null) { PurchaseOptionMoneyPolicy optionMoneyPolicy = source.Policies.OfType <PurchaseOptionMoneyPolicy>().FirstOrDefault <PurchaseOptionMoneyPolicy>(); if (optionMoneyPolicy != null && source.UnitListPrice.Amount != optionMoneyPolicy.SellPrice.Amount) { commercePrice.CurrencyCode = optionMoneyPolicy.SellPrice.CurrencyCode; commercePrice.ListPrice = optionMoneyPolicy.SellPrice.Amount; } else { commercePrice.CurrencyCode = source.UnitListPrice.CurrencyCode; commercePrice.ListPrice = source.UnitListPrice.Amount; } commercePrice.Amount = commercePrice.ListPrice; } commerceCartProduct.Price = (Price)commercePrice; destination.Product = (CartProduct)commerceCartProduct; }