public override async Task <CartLineComponent> Run(CartLineComponent arg, CommercePipelineExecutionContext context) { Condition.Requires(arg).IsNotNull($"{this.Name}: The line can not be null"); var productArgument = ProductArgument.FromItemId(arg.ItemId); SellableItem sellableItem = null; if (productArgument.IsValid()) { sellableItem = context.CommerceContext.GetEntity <SellableItem>(s => s.ProductId.Equals(productArgument.ProductId, StringComparison.OrdinalIgnoreCase)); } if (sellableItem == null) { return(null); } var productComponent = arg.GetComponent <CartProductComponent>(); productComponent.ItemType = sellableItem.TypeOfGood; return(arg); }
public virtual async Task<InventoryInformation> Process(CommerceContext commerceContext, InventoryModel inventory) { var inventorySetId = $"{CommerceEntity.IdPrefix<InventorySet>()}{inventory.InventorySet}"; string inventorySetIdPrefix = CommerceEntity.IdPrefix<InventorySet>(); var productArgument = ProductArgument.FromItemId(inventory.SellableItemId); string inventoryIdPrefix = CommerceEntity.IdPrefix<InventoryInformation>(); string inventorySetName = inventorySetId.Replace(inventorySetIdPrefix, string.Empty, StringComparison.OrdinalIgnoreCase); string friendlyId = string.IsNullOrWhiteSpace(productArgument.VariantId) ? $"{inventorySetName}-{productArgument.ProductId}" : $"{inventorySetName}-{productArgument.ProductId}-{productArgument.VariantId}"; string inventoryId = $"{inventoryIdPrefix}{friendlyId}"; string sellableItemId = $"{CommerceEntity.IdPrefix<SellableItem>()}{productArgument.ProductId}"; string variationId = productArgument.VariantId; var inventoryInformation = CreateInventoryInformation(inventory, inventoryId, friendlyId, inventorySetId); await AssociateInventoryInformationToSellableItem(commerceContext, inventorySetId, inventoryInformation); await UpdateSellableItem(commerceContext, sellableItemId, variationId, inventoryInformation, inventorySetId); await _inventoryCommander.PersistEntity(commerceContext, inventoryInformation); return inventoryInformation; }
public override async Task <ItemAvailabilityComponent> Run(ItemAvailabilityComponent arg, CommercePipelineExecutionContext context) { if (arg == null || string.IsNullOrEmpty(arg.ItemId)) { return(arg); } var productArgument = ProductArgument.FromItemId(arg.ItemId); var sellableItem = context.CommerceContext.GetEntity <SellableItem>(x => x.Id.Equals($"{CommerceEntity.IdPrefix<SellableItem>()}{productArgument.ProductId}", StringComparison.OrdinalIgnoreCase)) ?? await _commander.Pipeline <IGetSellableItemPipeline>().Run(productArgument, context).ConfigureAwait(false); if (sellableItem == null || !sellableItem.Tags.Any(t => t.Name.Equals("D365"))) { return(arg); } // This is needed if not having an inventory set assoicated to the sellable-item. // Without the InventoryComponent on the sellabl-item, inventory won't show on the PDP or within cart. if (!string.IsNullOrEmpty(productArgument.VariantId)) { sellableItem.GetComponent <InventoryComponent>(productArgument.VariantId); } else { sellableItem.GetComponent <InventoryComponent>(); } return(arg); }
public override async Task <CartLineComponent> Run(CartLineComponent arg, CommercePipelineExecutionContext context) { Condition.Requires(arg).IsNotNull($"{Name}: The CartLineComponent can not be null"); if (arg.HasComponent <LineItemProductExtendedComponent>()) { return(arg); } var productArgument = ProductArgument.FromItemId(arg.ItemId); if (!productArgument.IsValid()) { await RegisterLineIsNotPurchasableError(arg, context); return(null); } var sellableItem = context.CommerceContext.GetEntity <SellableItem>(s => s.ProductId.Equals(productArgument.ProductId, StringComparison.OrdinalIgnoreCase)); if (sellableItem == null) { await RegisterLineIsNotPurchasableError(arg, context); return(null); } var component = arg.GetComponent <LineItemProductExtendedComponent>(); component.Id = sellableItem.FriendlyId; component.Brand = sellableItem.Brand; component.ParentCategoryList = sellableItem.ParentCategoryList; return(arg); }
public virtual async Task <CommerceEntity> Process( CommerceContext commerceContext, string itemId, bool filterVariations) { using (CommandActivity.Start(commerceContext, this)) { CommercePipelineExecutionContextOptions pipelineContextOptions = commerceContext.GetPipelineContextOptions(); if (itemId.Contains("|")) { itemId = itemId.Replace("|", ","); } if (!string.IsNullOrEmpty(itemId)) { if (itemId.Split(',').Length == 3) { var strArray = itemId.Split(','); ProductArgument productArgument = new ProductArgument(strArray[0], strArray[1]) { VariantId = strArray[2] }; var sellableItem = await _pipeline.Run(productArgument, pipelineContextOptions).ConfigureAwait(false); var catalog = await _findEntityPipeline.Run(new FindEntityArgument(typeof(Catalog), CommerceEntity.IdPrefix <Catalog>() + productArgument.CatalogName), pipelineContextOptions).ConfigureAwait(false) as Catalog; if (catalog != null && sellableItem != null && sellableItem.HasPolicy <PriceCardPolicy>()) { var priceCardName = sellableItem.GetPolicy <PriceCardPolicy>(); string entityId = $"{CommerceEntity.IdPrefix<PriceCard>()}{catalog.PriceBookName}-{priceCardName.PriceCardName}"; CommerceEntity commerceEntity = await _findEntityPipeline.Run(new FindEntityArgument(typeof(PriceCard), entityId), pipelineContextOptions).ConfigureAwait(false); return(commerceEntity); } } } string str = await pipelineContextOptions.CommerceContext.AddMessage(commerceContext.GetPolicy <KnownResultCodes>().Error, "ItemIdIncorrectFormat", new object[] { itemId }, "Expecting a CatalogId and a ProductId in the ItemId: " + itemId); return(null); } }
private async Task <bool> IsGiftBoxAllowed(string itemId, CommercePipelineExecutionContext context) { var productArgument = ProductArgument.FromItemId(itemId); if (productArgument.IsValid()) { var sellableItem = await _getSellableItemPipeline.Run(productArgument, context); if (sellableItem != null) { return(sellableItem.GetComponent <SellableItemGiftBoxComponent>().AllowGiftBox); } } return(false); }
public override async Task <CartLineArgument> Run(CartLineArgument arg, CommercePipelineExecutionContext context) { ProductArgument productArgument = ProductArgument.FromItemId(arg.Line.ItemId); if (string.IsNullOrEmpty(productArgument.VariantId)) { return(arg); } var sellableItem = await this._commander.Command <GetSellableItemCommand>().Process(context.CommerceContext, productArgument.AsItemId(), true).ConfigureAwait(false); var variantComponent = sellableItem.GetVariation(productArgument.VariantId); if (!string.IsNullOrEmpty(variantComponent?.GetComponent <PersonalizationComponent>()?.PersonalizationId)) { arg.Line.ChildComponents.Add(variantComponent.GetComponent <PersonalizationComponent>()); } return(arg); }
protected async virtual Task <List <SellableItem> > GetSellableItemPerCurrency(string catalogName, string sellableItemId, string variantId, CommercePipelineExecutionContext context) { var sellableItems = new List <SellableItem>(); var currencySet = await Commander.Command <GetCurrencySetCommand>().Process(context.CommerceContext, context.GetPolicy <GlobalCurrencyPolicy>().DefaultCurrencySet).ConfigureAwait(false); if (!currencySet.HasComponent <CurrenciesComponent>()) { return(sellableItems); } var productArgument = new ProductArgument(catalogName, sellableItemId); if (!string.IsNullOrWhiteSpace(variantId)) { productArgument.VariantId = variantId; } var currencies = currencySet.GetComponent <CurrenciesComponent>().Currencies; var initialCurrency = context.CommerceContext.Headers["Currency"]; foreach (var currency in currencies) { var commerceContext = new CommerceContext(context.CommerceContext.Logger, context.CommerceContext.TelemetryClient) { Environment = context.CommerceContext.Environment, GlobalEnvironment = context.CommerceContext.GlobalEnvironment, Headers = context.CommerceContext.Headers }; commerceContext.Headers["Currency"] = currency.Code; var sellableItem = await Commander.Command <GetSellableItemCommand>().Process(commerceContext, productArgument.AsItemId(), false).ConfigureAwait(false); if (sellableItem != null) { sellableItems.Add(sellableItem); } } context.CommerceContext.Headers["Currency"] = initialCurrency; return(sellableItems); }
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); }
internal static List <KeyValuePair <string, decimal> > GetCartShippingRates(Cart cart, IGetSellableItemPipeline getSellableItemPipeline, CommercePipelineExecutionContext context) { var input = new UpsReqestInput(); UpsClientPolicy = context.GetPolicy <UpsClientPolicy>(); if (cart != null && cart.Lines.Any <CartLineComponent>() && cart.HasComponent <PhysicalFulfillmentComponent>()) { var component = cart.GetComponent <PhysicalFulfillmentComponent>(); var shippingParty = component?.ShippingParty; input.AddressLine1 = shippingParty.Address1; input.AddressLine2 = shippingParty.Address2; input.City = shippingParty.City; input.CountryCode = shippingParty.CountryCode; input.StateCode = shippingParty.StateCode; input.ZipPostalCode = shippingParty.ZipPostalCode; input.PriceValue = cart.Totals.SubTotal.Amount; decimal height = 0.0M; decimal width = 0.0M; decimal length = 0.0m; decimal weight = 0.0m; foreach (var cartLineComponent in cart.Lines) { // get specific weight value var productArgument = ProductArgument.FromItemId(cartLineComponent.ItemId); if (!productArgument.IsValid()) { continue; } var sellableItem = getSellableItemPipeline.Run(productArgument, context).Result; var product = context.CommerceContext.Objects.OfType <Product>().FirstOrDefault <Product>((Product p) => p.ProductId.Equals(sellableItem.FriendlyId, StringComparison.OrdinalIgnoreCase)); decimal val = 0m; if (product != null) { if (product.HasProperty(UpsClientPolicy.WeightFieldName) && product[UpsClientPolicy.WeightFieldName].ToString().Trim() != "") { val = GetFirstDecimalFromString(product[UpsClientPolicy.WeightFieldName].ToString()); } else { val = GetFirstDecimalFromString(UpsClientPolicy.Weight); } if (val > 0) { weight += val; } val = product.HasProperty(UpsClientPolicy.HeightFieldName) && product[UpsClientPolicy.HeightFieldName].ToString().Trim() != "" ? GetFirstDecimalFromString(product[UpsClientPolicy.HeightFieldName].ToString()) : GetFirstDecimalFromString(UpsClientPolicy.Height); if (val > 0) { height += val; } val = product.HasProperty(UpsClientPolicy.WidthFieldName) && product[UpsClientPolicy.WidthFieldName].ToString().Trim() != "" ? GetFirstDecimalFromString(product[UpsClientPolicy.WidthFieldName].ToString()) : GetFirstDecimalFromString(UpsClientPolicy.Width); if (val > 0 && val > width) { width = val; } val = product.HasProperty(UpsClientPolicy.LengthFieldName) && product[UpsClientPolicy.LengthFieldName].ToString().Trim() != "" ? GetFirstDecimalFromString(product[UpsClientPolicy.LengthFieldName].ToString()) : GetFirstDecimalFromString(UpsClientPolicy.Length); if (val > 0 && val > length) { length = val; } } } input.Height = Math.Ceiling(height).ToString(CultureInfo.CurrentCulture); input.Width = Math.Ceiling(width).ToString(CultureInfo.CurrentCulture); input.Length = Math.Ceiling(length).ToString(CultureInfo.CurrentCulture); input.Weight = weight; } var rates = new List <KeyValuePair <string, decimal> >(); rates = GetShippingRates(input, context); return(rates); }
internal static List <KeyValuePair <string, decimal> > GetCartShippingRates(Cart cart, GetProductCommand getProductCommand, FedExClientPolicy fedExClientPolicy, CommerceContext commerceContext) { var input = new FedexReqestInput(); FedExClientPolicy = fedExClientPolicy; if (cart != null && cart.Lines.Any <CartLineComponent>() && cart.HasComponent <PhysicalFulfillmentComponent>()) { var component = cart.GetComponent <PhysicalFulfillmentComponent>(); var shippingParty = component?.ShippingParty; input.AddressLine1 = shippingParty.Address1; input.AddressLine2 = shippingParty.Address2; input.City = shippingParty.City; input.CountryCode = shippingParty.CountryCode; input.StateCode = shippingParty.StateCode; input.ZipPostalCode = shippingParty.ZipPostalCode; input.PriceValue = cart.Totals.SubTotal.Amount; decimal height = 0.0M; decimal width = 0.0M; decimal length = 0.0m; decimal weight = 0.0m; foreach (var cartLineComponent in cart.Lines) { // get specific weight value var productArgument = ProductArgument.FromItemId(cartLineComponent.ItemId); if (!productArgument.IsValid()) { continue; } var product = getProductCommand.Process(commerceContext, productArgument.CatalogName, productArgument.ProductId).Result; decimal val = 0m; if (product != null) { if (product.HasProperty(FedExClientPolicy.WeightFieldName) && product[FedExClientPolicy.WeightFieldName].ToString().Trim() != "") { val = GetFirstDecimalFromString(product[FedExClientPolicy.WeightFieldName].ToString()); } else { val = GetFirstDecimalFromString(FedExClientPolicy.Weight); } if (val > 0) { weight += val; } val = product.HasProperty(FedExClientPolicy.HeightFieldName) && product[FedExClientPolicy.HeightFieldName].ToString().Trim() != "" ? GetFirstDecimalFromString(product[FedExClientPolicy.HeightFieldName].ToString()) : GetFirstDecimalFromString(FedExClientPolicy.Height); if (val > 0) { height += val; } val = product.HasProperty(FedExClientPolicy.WidthFieldName) && product[FedExClientPolicy.WidthFieldName].ToString().Trim() != "" ? GetFirstDecimalFromString(product[FedExClientPolicy.WidthFieldName].ToString()) : GetFirstDecimalFromString(FedExClientPolicy.Width); if (val > 0 && val > width) { width = val; } val = product.HasProperty(FedExClientPolicy.LengthFieldName) && product[FedExClientPolicy.LengthFieldName].ToString().Trim() != "" ? GetFirstDecimalFromString(product[FedExClientPolicy.LengthFieldName].ToString()) : GetFirstDecimalFromString(FedExClientPolicy.Length); if (val > 0 && val > length) { length = val; } } } input.Height = Math.Ceiling(height).ToString(CultureInfo.CurrentCulture); input.Width = Math.Ceiling(width).ToString(CultureInfo.CurrentCulture); input.Length = Math.Ceiling(length).ToString(CultureInfo.CurrentCulture); input.Weight = Math.Ceiling(weight); } var rates = new List <KeyValuePair <string, decimal> >(); if (input.CountryCode.ToLower() == "us") { rates = FedxUs.GetShippingRates(input, FedExClientPolicy); } else { rates = FedExInternational.GetShippingRates(input, FedExClientPolicy); } return(rates); }