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; }
private async Task <bool?> CalculateCartLinePrice( CartLineComponent arg, CommercePipelineExecutionContext context) { ProductArgument productArgument = ProductArgument.FromItemId(arg.ItemId); SellableItem sellableItem = null; if (productArgument.IsValid()) { sellableItem = context.CommerceContext.GetEntity((Func <SellableItem, bool>)(s => s.ProductId.Equals(productArgument.ProductId, StringComparison.OrdinalIgnoreCase))); if (sellableItem == null) { string simpleName = productArgument.ProductId.SimplifyEntityName(); sellableItem = context.CommerceContext.GetEntity((Func <SellableItem, bool>)(s => s.ProductId.Equals(simpleName, StringComparison.OrdinalIgnoreCase))); if (sellableItem != null) { sellableItem.ProductId = simpleName; } } } if (sellableItem == null) { CommercePipelineExecutionContext executionContext = context; CommerceContext commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; object[] args = new object[] { arg.ItemId }; string defaultMessage = "Item '" + arg.ItemId + "' is not purchasable."; executionContext.Abort(await commerceContext.AddMessage(error, "LineIsNotPurchasable", args, defaultMessage), context); executionContext = null; return(new bool?()); } MessagesComponent messagesComponent = arg.GetComponent <MessagesComponent>(); messagesComponent.Clear(context.GetPolicy <KnownMessageCodePolicy>().Pricing); if (sellableItem.HasComponent <MessagesComponent>()) { List <MessageModel> messages = sellableItem.GetComponent <MessagesComponent>().GetMessages(context.GetPolicy <KnownMessageCodePolicy>().Pricing); messagesComponent.AddMessages(messages); } arg.UnitListPrice = sellableItem.ListPrice; string listPriceMessage = "CartItem.ListPrice<=SellableItem.ListPrice: Price=" + arg.UnitListPrice.AsCurrency(false, null); string sellPriceMessage = string.Empty; PurchaseOptionMoneyPolicy optionMoneyPolicy = new PurchaseOptionMoneyPolicy(); if (sellableItem.HasPolicy <PurchaseOptionMoneyPolicy>()) { optionMoneyPolicy.SellPrice = sellableItem.GetPolicy <PurchaseOptionMoneyPolicy>().SellPrice; sellPriceMessage = "CartItem.SellPrice<=SellableItem.SellPrice: Price=" + optionMoneyPolicy.SellPrice.AsCurrency(false, null); } PriceSnapshotComponent snapshotComponent; if (sellableItem.HasComponent <ItemVariationsComponent>()) { ItemVariationSelectedComponent lineVariant = arg.ChildComponents.OfType <ItemVariationSelectedComponent>().FirstOrDefault(); ItemVariationsComponent itemVariationsComponent = sellableItem.GetComponent <ItemVariationsComponent>(); ItemVariationComponent itemVariationComponent; if (itemVariationsComponent == null) { itemVariationComponent = null; } else { IList <Component> childComponents = itemVariationsComponent.ChildComponents; itemVariationComponent = childComponents?.OfType <ItemVariationComponent>().FirstOrDefault(v => { return(!string.IsNullOrEmpty(v.Id) ? v.Id.Equals(lineVariant?.VariationId, StringComparison.OrdinalIgnoreCase) : false); }); } if (itemVariationComponent != null) { if (itemVariationComponent.HasComponent <MessagesComponent>()) { List <MessageModel> messages = itemVariationComponent.GetComponent <MessagesComponent>().GetMessages(context.GetPolicy <KnownMessageCodePolicy>().Pricing); messagesComponent.AddMessages(messages); } arg.UnitListPrice = itemVariationComponent.ListPrice; listPriceMessage = "CartItem.ListPrice<=SellableItem.Variation.ListPrice: Price=" + arg.UnitListPrice.AsCurrency(false, null); if (itemVariationComponent.HasPolicy <PurchaseOptionMoneyPolicy>()) { optionMoneyPolicy.SellPrice = itemVariationComponent.GetPolicy <PurchaseOptionMoneyPolicy>().SellPrice; sellPriceMessage = "CartItem.SellPrice<=SellableItem.Variation.SellPrice: Price=" + optionMoneyPolicy.SellPrice.AsCurrency(false, null); } } snapshotComponent = itemVariationComponent != null?itemVariationComponent.ChildComponents.OfType <PriceSnapshotComponent>().FirstOrDefault() : null; } else { snapshotComponent = sellableItem.Components.OfType <PriceSnapshotComponent>().FirstOrDefault(); } string currentCurrency = context.CommerceContext.CurrentCurrency(); PriceTier priceTier = snapshotComponent?.Tiers.OrderByDescending(t => t.Quantity).FirstOrDefault(t => { return(t.Currency.Equals(currentCurrency, StringComparison.OrdinalIgnoreCase) ? t.Quantity <= arg.Quantity : false); }); Customer customer = await _findEntityPipeline.Run(new FindEntityArgument(typeof(Customer), context.CommerceContext.CurrentCustomerId(), false), context) as Customer; bool isMembershipLevelPrice = false; if (customer != null && customer.HasComponent <MembershipSubscriptionComponent>()) { var membershipSubscriptionComponent = customer.GetComponent <MembershipSubscriptionComponent>(); var membershipLevel = membershipSubscriptionComponent.MemerbshipLevelName; if (snapshotComponent != null && snapshotComponent.HasComponent <MembershipTiersComponent>()) { var membershipTiersComponent = snapshotComponent.GetComponent <MembershipTiersComponent>(); var membershipPriceTier = membershipTiersComponent.Tiers.FirstOrDefault(x => x.MembershipLevel == membershipLevel); if (membershipPriceTier != null) { optionMoneyPolicy.SellPrice = new Money(membershipPriceTier.Currency, membershipPriceTier.Price); isMembershipLevelPrice = true; sellPriceMessage = string.Format("CartItem.SellPrice<=PriceCard.ActiveSnapshot: MembershipLevel={0}|Price={1}|Qty={2}", membershipSubscriptionComponent.MemerbshipLevelName, optionMoneyPolicy.SellPrice.AsCurrency(false, null), membershipPriceTier.Quantity); } } } if (!isMembershipLevelPrice && priceTier != null) { optionMoneyPolicy.SellPrice = new Money(priceTier.Currency, priceTier.Price); sellPriceMessage = string.Format("CartItem.SellPrice<=PriceCard.ActiveSnapshot: Price={0}|Qty={1}", optionMoneyPolicy.SellPrice.AsCurrency(false, null), priceTier.Quantity); } arg.Policies.Remove(arg.Policies.OfType <PurchaseOptionMoneyPolicy>().FirstOrDefault()); if (optionMoneyPolicy.SellPrice == null) { return(false); } arg.SetPolicy(optionMoneyPolicy); if (!string.IsNullOrEmpty(sellPriceMessage)) { messagesComponent.AddMessage(context.GetPolicy <KnownMessageCodePolicy>().Pricing, sellPriceMessage); } if (!string.IsNullOrEmpty(listPriceMessage)) { messagesComponent.AddMessage(context.GetPolicy <KnownMessageCodePolicy>().Pricing, listPriceMessage); } return(true); }
public override async Task <Cart> Run(Cart arg, CommercePipelineExecutionContext context) { Condition.Requires(arg).IsNotNull($"{Name}: Cart cannot be null."); Condition.Requires(arg.Lines).IsNotNull($"{Name}: The cart's lines cannot be null"); if (!arg.Lines.Any()) { return(arg); } // Valid cart items var validCartLines = arg.Lines .Where(line => line != null && line.HasComponent <CartProductComponent>() && !string.IsNullOrEmpty(line.ItemId) && line.ItemId.Split('|').Length >= 2); // Iterate valid cart items foreach (var line in validCartLines) { // Get sellable items (products) SellableItem sellableItem = await _commerceCommander.Command <GetSellableItemCommand>().Process(context.CommerceContext, line.ItemId, false); if (sellableItem == null) { context.Logger.LogError($"{Name}-SellableItemNotFound for Cart Line: ItemId={line.ItemId}|CartId={arg.Id}|LineId={line.Id}"); return(arg); } // Set list & sell pricing line.UnitListPrice = sellableItem.ListPrice; if (sellableItem.Manufacturer.Equals("CMYKhub", StringComparison.OrdinalIgnoreCase)) { var request = new { ProductId = sellableItem.FriendlyId, Quantity = (int)line.Quantity, Kinds = 1 }; var cmykHubPrice = await CreatePriceAsync(request); if (cmykHubPrice != null) { var optionMoneyPolicy = new PurchaseOptionMoneyPolicy { SellPrice = new Sitecore.Commerce.Core.Money("USD", cmykHubPrice.Price.IncTax) // new Sitecore.Commerce.Core.Money(cmykHubPrice.Price.Currency.Code, cmykHubPrice.Price.IncTax); }; line.Policies.Remove(line.Policies.OfType <PurchaseOptionMoneyPolicy>().FirstOrDefault()); line.SetPolicy(optionMoneyPolicy); // Add to pricing messaging var lineMessages = line.GetComponent <MessagesComponent>(); lineMessages.AddMessage(context.GetPolicy <KnownMessageCodePolicy>().Pricing, $"CartItem.SellPrice<=CMYKhub.Pricing: Price={optionMoneyPolicy.SellPrice.AsCurrency()}"); } } ; } return(arg); }