public void YieldCartLines_08_Multiple( IRuleValue <string> targetTag, Cart cart, CartProductComponent component, CommerceContext commerceContext, IRuleExecutionContext context) { /********************************************** * Arrange **********************************************/ context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); component.Tags.Add(new Tag("Smartphone"));; cart.Lines.ForEach(l => l.SetComponent(component)); commerceContext.AddObject(cart); targetTag.Yield(context).ReturnsForAnyArgs("Smartphone"); /********************************************** * Act **********************************************/ var matchingLines = targetTag.YieldCartLinesWithTag(context); /********************************************** * Assert **********************************************/ matchingLines.Should().HaveCount(3); }
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); }
public void YieldCartLines_06_EmptyTag( IRuleValue <string> targetTag, Cart cart, CartProductComponent component, CommerceContext commerceContext, IRuleExecutionContext context) { /********************************************** * Arrange **********************************************/ context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); component.Tags.Add(new Tag("Smartphone")); cart.Lines[1].SetComponent(component); commerceContext.AddObject(cart); targetTag.Yield(context).ReturnsForAnyArgs(""); /********************************************** * Act **********************************************/ IEnumerable <CartLineComponent> matchingLines = null; Action executeAction = () => matchingLines = targetTag.YieldCartLinesWithTag(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); matchingLines.Should().BeEmpty(); }
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; }
//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); } } }
/// <summary> /// The execute. /// </summary> /// <param name="arg"> /// The pipeline argument. /// </param> /// <param name="context"> /// The context. /// </param> /// <returns> /// The <see cref="PipelineArgument"/>. /// </returns> public override Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context) { Condition.Requires(entityView).IsNotNull($"{this.Name}: The argument can not be null"); var knownCartViewsPolicy = context.GetPolicy <KnownCartViewsPolicy>(); EntityViewArgument request = context.CommerceContext.GetObject <EntityViewArgument>(); if (string.IsNullOrEmpty(request?.ViewName) || !request.ViewName.Equals(knownCartViewsPolicy.Master, StringComparison.OrdinalIgnoreCase) || !(request.Entity is Cart)) { return(Task.FromResult(entityView)); } Cart cart = (Cart)request.Entity; EntityView linesView = new EntityView(); linesView.EntityId = cart.Id; linesView.Name = knownCartViewsPolicy.CartLinesView; linesView.UiHint = "Table"; entityView.ChildViews.Add(linesView); List <CartLineComponent> cartLineComponentList = new List <CartLineComponent>(); var cartLines = cart.Lines; foreach (CartLineComponent line in cartLines) { EntityView lineView = new EntityView(); lineView.EntityId = linesView.EntityId; lineView.ItemId = line.Id; lineView.Name = "LineItemDetails"; this.PopulateLineChildView(lineView, line, context); CartProductComponent component = line.GetComponent <CartProductComponent>(); EntityView sellableItemView = new EntityView(); sellableItemView.EntityId = component.Id; sellableItemView.DisplayName = component.DisplayName; lineView.ChildViews.Add(sellableItemView); linesView.ChildViews.Add((Model)lineView); } return(Task.FromResult(entityView)); }
public void Execute_HasMatchingLines_ShouldApplyCartLineAdjustment( string targetTag, bool autoRemove, Cart cart, CartProductComponent cartProductComponent, CartTotals cartTotals, CommerceContext commerceContext, IRuleExecutionContext context) { /********************************************** * Arrange **********************************************/ ApplyFreeGiftDiscountCommand discountCommand; ApplyFreeGiftEligibilityCommand eligibilityCommand; ApplyFreeGiftAutoRemoveCommand autoRemoveCommand; CartItemTargetTagFreeGiftAction action = BuildAction(out discountCommand, out eligibilityCommand, out autoRemoveCommand); commerceContext.AddObject(cartTotals); commerceContext.AddObject(cart); cartProductComponent.Tags.Add(new Tag(targetTag)); cart.Adjustments.Clear(); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.Lines[0].SetComponent(cartProductComponent); action.TargetTag = Substitute.For <IRuleValue <string> >(); action.TargetTag.Yield(context).ReturnsForAnyArgs(targetTag); action.AutoRemove = Substitute.For <IRuleValue <bool> >(); action.AutoRemove.Yield(context).ReturnsForAnyArgs(autoRemove); context.Fact(Arg.Any <IFactIdentifier>()).Returns(commerceContext); /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); eligibilityCommand.Received().Process(Arg.Any <CommerceContext>(), cart, Arg.Any <string>()); discountCommand.Received().Process(Arg.Any <CommerceContext>(), Arg.Any <CartLineComponent>(), Arg.Any <string>()); }
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); } } }
private void AddLineItemView(EntityView entityView, CommerceContext commerceContext, Cart cart) { if (cart == null) { return; } var lineItems = new EntityView { EntityId = entityView.EntityId, ItemId = string.Empty, DisplayName = "Line Items", Name = "Line Items", UiHint = "Table" }; entityView.ChildViews.Add(lineItems); var cartProductComponents = new EntityView { EntityId = string.Empty, ItemId = string.Empty, DisplayName = "CartProductComponents", Name = "CartProductComponent", UiHint = "Table" }; entityView.ChildViews.Add(cartProductComponents); foreach (var line in cart.Lines) { var lineView = new EntityView { EntityId = cart.Id, ItemId = line.ItemId, Name = line.Name, DisplayName = line.Name, UiHint = "Flat" }; lineItems.ChildViews.Add(lineView); lineView.Properties.Add(new ViewProperty { Name = "Name", RawValue = line.Name }); lineView.Properties.Add(new ViewProperty { Name = "ItemId", RawValue = line.ItemId }); lineView.Properties.Add(new ViewProperty { Name = "Quantity", RawValue = line.Quantity }); lineView.Properties.Add(new ViewProperty { Name = "Unit List Price", RawValue = line.UnitListPrice }); lineView.Properties.Add(new ViewProperty { Name = "SubTotal", RawValue = line.Totals.SubTotal }); lineView.Properties.Add(new ViewProperty { Name = "AdjustmentsTotal", RawValue = line.Totals.AdjustmentsTotal }); lineView.Properties.Add(new ViewProperty { Name = "GrandTotal", RawValue = line.Totals.GrandTotal }); CartProductComponent cartProductComponent = (CartProductComponent)line.CartLineComponents.FirstOrDefault(element => element is CartProductComponent); ItemAvailabilityComponent itemAvailabilityComponent = (ItemAvailabilityComponent)line.CartLineComponents.FirstOrDefault(element => element is ItemAvailabilityComponent); var cartProductEntityView = new EntityView { EntityId = CommerceEntity.IdPrefix <SellableItem>() + cartProductComponent.Id, ItemId = CommerceEntity.IdPrefix <SellableItem>() + cartProductComponent.Id, Name = line.Id, DisplayName = line.Name, UiHint = "Flat" }; cartProductComponents.ChildViews.Add(cartProductEntityView); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Name", RawValue = cartProductComponent.Name, UiType = "EntityLink" }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Id", RawValue = cartProductComponent.Id }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Catalog", RawValue = cartProductComponent.Catalog }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Product Url", RawValue = cartProductComponent.ProductUrl }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Product Name", RawValue = cartProductComponent.ProductName }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Product Image", RawValue = cartProductComponent.Image.SitecoreId, }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Tags", RawValue = string.Join("|", cartProductComponent.Tags.Select(element => element.Name)) }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Availability Quantity", RawValue = itemAvailabilityComponent.AvailableQuantity }); cartProductEntityView.Properties.Add(new ViewProperty { Name = "Is Available", RawValue = itemAvailabilityComponent.IsAvailable }); } }
private void PopulateLineChildView(EntityView lineEntityView, CartLineComponent line, CommercePipelineExecutionContext context) { if (line == null || lineEntityView == null || context == null) { return; } LineQuantityPolicy policy = context.GetPolicy <LineQuantityPolicy>(); ViewProperty itemIdProperty = new ViewProperty(); itemIdProperty.Name = "ItemId"; itemIdProperty.IsHidden = true; itemIdProperty.IsReadOnly = true; itemIdProperty.RawValue = (object)line.Id; lineEntityView.Properties.Add(itemIdProperty); ViewProperty listPriceProperty = new ViewProperty(); listPriceProperty.Name = "ListPrice"; listPriceProperty.IsReadOnly = true; listPriceProperty.RawValue = (object)line.UnitListPrice; lineEntityView.Properties.Add(listPriceProperty); ViewProperty sellPriceProperty = new ViewProperty(); sellPriceProperty.Name = "SellPrice"; sellPriceProperty.IsReadOnly = true; sellPriceProperty.RawValue = (object)line.GetPolicy <PurchaseOptionMoneyPolicy>().SellPrice; lineEntityView.Properties.Add(sellPriceProperty); ViewProperty quantityProperty = new ViewProperty(); quantityProperty.Name = "Quantity"; quantityProperty.IsReadOnly = true; quantityProperty.RawValue = (object)(policy.AllowDecimal ? line.Quantity : (Decimal)(int)line.Quantity); quantityProperty.OriginalType = policy.AllowDecimal ? typeof(Decimal).FullName : typeof(int).FullName; lineEntityView.Properties.Add(quantityProperty); ViewProperty subtotalProperty = new ViewProperty(); subtotalProperty.Name = "Subtotal"; subtotalProperty.IsReadOnly = true; subtotalProperty.RawValue = (object)line.Totals.SubTotal; lineEntityView.Properties.Add(subtotalProperty); ViewProperty adjustmentsProperty = new ViewProperty(); adjustmentsProperty.Name = "Adjustments"; adjustmentsProperty.IsReadOnly = true; adjustmentsProperty.RawValue = (object)line.Totals.AdjustmentsTotal; lineEntityView.Properties.Add(adjustmentsProperty); ViewProperty lineTotalProperty = new ViewProperty(); lineTotalProperty.Name = "LineTotal"; lineTotalProperty.IsReadOnly = true; lineTotalProperty.RawValue = (object)line.Totals.GrandTotal; lineEntityView.Properties.Add(lineTotalProperty); CartProductComponent component = line.GetComponent <CartProductComponent>(); ViewProperty sellableItemNameProperty = new ViewProperty(); sellableItemNameProperty.Name = "Name"; sellableItemNameProperty.IsReadOnly = true; sellableItemNameProperty.RawValue = (object)component.DisplayName; lineEntityView.Properties.Add(sellableItemNameProperty); ViewProperty sizeProperty = new ViewProperty(); sizeProperty.Name = "Size"; sizeProperty.IsReadOnly = true; sizeProperty.RawValue = (object)component.Size; lineEntityView.Properties.Add(sizeProperty); ViewProperty colourProperty = new ViewProperty(); colourProperty.Name = "Color"; colourProperty.IsReadOnly = true; colourProperty.RawValue = (object)component.Color; lineEntityView.Properties.Add(colourProperty); ViewProperty styleProperty = new ViewProperty(); styleProperty.Name = "Style"; styleProperty.IsReadOnly = true; styleProperty.RawValue = (object)component.Style; lineEntityView.Properties.Add(styleProperty); ViewProperty variationProperty = new ViewProperty(); variationProperty.Name = "Variation"; variationProperty.IsReadOnly = true; variationProperty.RawValue = line.HasComponent <ItemVariationSelectedComponent>() ? (object)line.GetComponent <ItemVariationSelectedComponent>().VariationId : (object)string.Empty; lineEntityView.Properties.Add(variationProperty); }