public override async Task <IEnumerable <Promotion> > Run(IEnumerable <Promotion> arg,
                                                                  CommercePipelineExecutionContext context)
        {
            var cart = context.CommerceContext != null?context.CommerceContext.GetObject <Cart>() : null;

            if (cart == null)
            {
                cart = context.CommerceContext != null ? (Cart)context.CommerceContext.GetObjects <FoundEntity>()?.FirstOrDefault(x => x.Entity.GetType() == typeof(Cart))?.Entity : null;
            }

            if (cart != null && cart.Id != QualifiedItemsArgument.TempCartId)
            {
                var qualifiedItemsArgument = context.CommerceContext.GetObject <QualifiedItemsArgument>();
                if (qualifiedItemsArgument == null)
                {
                    qualifiedItemsArgument = new QualifiedItemsArgument(true);
                }
                else
                {
                    qualifiedItemsArgument.QualifyQuantityThresholds = true;
                    context.CommerceContext.RemoveObject(qualifiedItemsArgument);
                }

                context.CommerceContext.AddObject(qualifiedItemsArgument);
            }


            return(await Task.FromResult(arg));
        }
        public override Task <SellableItem> Run(SellableItem sellableItem, CommercePipelineExecutionContext context)
        {
            if (context.CommerceContext != null)
            {
                var tempCart = new Cart(QualifiedItemsArgument.TempCartId);

                CartLineComponent line = new CartLineComponent()
                {
                    ItemId   = "TestCatalog|TestProduct|Variant2",
                    Quantity = 1,
                    Id       = sellableItem.Id
                };
                line.CartLineComponents.Add(new ItemAvailabilityComponent
                {
                    AvailableDate       = DateTimeOffset.MinValue,
                    AvailabilityExpires = DateTimeOffset.MaxValue,
                    AvailableQuantity   = 1,
                    IsAvailable         = true,
                    ItemId = line.ItemId
                });
                tempCart.Lines.Add(line);

                var cart = context.CommerceContext.GetObject <Cart>();
                if (cart == null)
                {
                    var entities = context.CommerceContext.GetObjects <FoundEntity>();
                    cart = entities?.FirstOrDefault(x =>
                                                    x.Entity != null && x.Entity.GetType() == typeof(Cart))?.Entity as Cart;
                }

                if (cart == null)
                {
                    context.CommerceContext.AddObject(tempCart);

                    var qualifiedItemsArgument = context.CommerceContext.GetObject <QualifiedItemsArgument>();
                    if (qualifiedItemsArgument != null)
                    {
                        qualifiedItemsArgument.QualifyQuantityThresholds = false;
                        context.CommerceContext.RemoveObject(qualifiedItemsArgument);
                        context.CommerceContext.AddObject(qualifiedItemsArgument);
                    }
                    else
                    {
                        qualifiedItemsArgument = new QualifiedItemsArgument(false);
                        context.CommerceContext.AddObject(qualifiedItemsArgument);
                        var promotions = _calculateCartPipeline.Run(tempCart, context).Result;


                        var qualifiedBenefits = qualifiedItemsArgument.RuleMetArguments
                                                .Where(x => x.PercentageOff != null).ToList();

                        foreach (var qualifiedBenefit in qualifiedBenefits)
                        {
                            sellableItem.Policies.Add(new QualifiedItemPercentageDiscountPolicy
                            {
                                QuantityFrom  = qualifiedBenefit.QuantityFrom,
                                QuantityTo    = qualifiedBenefit.QuantityTo,
                                PercentageOff = Convert.ToDecimal(qualifiedBenefit.PercentageOff)
                            });
                        }
                    }
                }
            }


            return(Task.FromResult(sellableItem));
        }