示例#1
0
        public override async Task <Cart> Run(CartLineArgument arg, CommercePipelineExecutionContext context)
        {
            AddWishListLineBlock addCartLineBlock = this;

            Condition.Requires <CartLineArgument>(arg).IsNotNull <CartLineArgument>(string.Format("{0}: The argument cannot be null.", addCartLineBlock.Name));
            Condition.Requires <Cart>(arg.Cart).IsNotNull <Cart>(string.Format("{0}: The cart cannot be null.", addCartLineBlock.Name));
            Condition.Requires <CartLineComponent>(arg.Line).IsNotNull <CartLineComponent>(string.Format("{0}: The line to add cannot be null.", addCartLineBlock.Name));
            context.CommerceContext.AddObject((object)arg);
            Cart cart = arg.Cart;

            var cartComponent = cart.GetComponent <CartTypeComponent>();

            if (cartComponent == null)
            {
                var cartTypeComponent = new CartTypeComponent();
                cartTypeComponent.CartType = CartTypeEnum.Wishlist.ToString();
                cart.SetComponent(cartTypeComponent);
            }
            cartComponent.CartType = CartTypeEnum.Wishlist.ToString();


            LineQuantityPolicy lineQuantityPolicy = context.GetPolicy <LineQuantityPolicy>();
            Decimal            quantity           = arg.Line.Quantity;
            CartLineComponent  existingLine       = cart.Lines.FirstOrDefault <CartLineComponent>((Func <CartLineComponent, bool>)(l => l.ItemId.Equals(arg.Line.ItemId, StringComparison.Ordinal)));
            bool flag1 = !await lineQuantityPolicy.IsValid(quantity, context.CommerceContext);

            if (!flag1)
            {
                bool flag2 = existingLine != null;
                if (flag2)
                {
                    flag2 = !await lineQuantityPolicy.IsValid(quantity + existingLine.Quantity, context.CommerceContext);
                }
                flag1 = flag2;
            }
            if (flag1)
            {
                context.Abort("Invalid or missing value for property 'Quantity'.", (object)context);
                return(cart);
            }
            if (!string.IsNullOrEmpty(arg.Line.ItemId))
            {
                if (arg.Line.ItemId.Split('|').Length >= 3)
                {
                    if (string.IsNullOrEmpty(arg.Line.Id))
                    {
                        arg.Line.Id = Guid.NewGuid().ToString("N");
                    }
                    List <CartLineComponent> list = cart.Lines.ToList <CartLineComponent>();
                    if (!context.CommerceContext.GetPolicy <RollupCartLinesPolicy>().Rollup)
                    {
                        list.Add(arg.Line);
                        context.CommerceContext.AddModel((Model) new LineAdded(arg.Line.Id, false));
                    }
                    else if (existingLine != null)
                    {
                        existingLine.Quantity += arg.Line.Quantity;
                        arg.Line.Id            = existingLine.Id;
                        context.CommerceContext.AddModel((Model) new LineUpdated(arg.Line.Id));
                    }
                    else
                    {
                        list.Add(arg.Line);
                        context.CommerceContext.AddModel((Model) new LineAdded(arg.Line.Id, false));
                    }
                    cart.Lines = (IList <CartLineComponent>)list;
                    return(cart);
                }
            }
            CommercePipelineExecutionContext executionContext = context;
            CommerceContext commerceContext = context.CommerceContext;
            string          error           = context.GetPolicy <KnownResultCodes>().Error;
            string          commerceTermKey = "ItemIdIncorrectFormat";

            object[] args = new object[1]
            {
                (object)arg.Line.ItemId
            };
            string defaultMessage = string.Format("Expecting a CatalogId and a ProductId in the ItemId: {0}.", (object)arg.Line.ItemId);

            executionContext.Abort(await commerceContext.AddMessage(error, commerceTermKey, args, defaultMessage), (object)context);
            executionContext = (CommercePipelineExecutionContext)null;
            return(cart);
        }
示例#2
0
        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);
        }