public override async Task <ProductComparison> Run(AddToProductCompareArgument arg, CommercePipelineExecutionContext context)
        {
            Contract.Requires(arg != null);
            Contract.Requires(context != null);

            Condition.Requires(arg).IsNotNull($"{Name}: The arg can not be null");
            Condition.Requires(arg.ProductId).IsNotNull($"{Name}: The product id can not be null");
            Condition.Requires(arg.CompareCollection).IsNotNull($"{Name}: The Compare Collection can not be null");

            var sellableItem = await _getSellableItemPipeline.Run(BuildProductArgument(arg), context).ConfigureAwait(false);

            if (sellableItem == null)
            {
                context.Logger.LogWarning($"ProductCompare: Unable to find sellable item to add to collection:{arg.CatalogName}-{arg.ProductId}-{arg.VariantId}");
                return(arg.CompareCollection);
            }

            var list = arg.CompareCollection.Products.ToList();

            if (list.Any(x => x.Id == sellableItem.Id))
            {
                context.Logger.LogDebug($"{Name}: SellableItem already exists in compare collection, no further action to take");
                return(arg.CompareCollection);
            }

            var addArg = new ListEntitiesArgument(new List <string> {
                sellableItem.Id
            }, arg.CompareCollection.Name);
            await _addListEntitiesPipeline.Run(addArg, context).ConfigureAwait(false);

            return(arg.CompareCollection);
        }
        public override async Task <SynchronizeProductArgument> Run(SynchronizeProductArgument arg, CommercePipelineExecutionContext context)
        {
            Sitecore.Commerce.Plugin.Catalog.SellableItem product = null;
            var productId = arg.ImportProduct.ProductId.ProposeValidId()
                            .EnsurePrefix(CommerceEntity.IdPrefix <Sitecore.Commerce.Plugin.Catalog.SellableItem>());

            if (await _doesEntityExistPipeline.Run(
                    new FindEntityArgument(typeof(Sitecore.Commerce.Plugin.Catalog.SellableItem), productId),
                    context.CommerceContext.GetPipelineContextOptions()))
            {
                product = await _getSellableItemPipeline.Run(new ProductArgument(arg.Catalog.Id, productId),
                                                             context.CommerceContext.GetPipelineContextOptions());
            }
            else
            {
                var productName  = arg.ImportProduct.ProductName.FirstOrDefault()?.Name;
                var createResult = await _createSellableItemPipeline.Run(
                    new CreateSellableItemArgument(arg.ImportProduct.ProductId.ProposeValidId(), arg.ImportProduct.ProductId,
                                                   productName, ""), context.CommerceContext.GetPipelineContextOptions());

                product = createResult?.SellableItems?.FirstOrDefault(s => s.Id.Equals(productId));
            }

            Condition.Requires <Sitecore.Commerce.Plugin.Catalog.SellableItem>(product)
            .IsNotNull($"{this.Name}: The Product could not be created.");

            product.IsPersisted = true;
            arg.SellableItem    = product;

            return(arg);
        }
예제 #3
0
        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);
        }
        /// <summary>
        /// Ensures the SellableItem exists and creates it if it doesn't.
        /// Adds/updates the product identifiers and Brand.
        /// </summary>
        /// <param name="arg">The arg.</param>
        /// <param name="context">The context.</param>
        /// <returns>The arg with an updated SellableItem property.</returns>
        public override async Task <SynchronizeProductArgument> Run(SynchronizeProductArgument arg, CommercePipelineExecutionContext context)
        {
            var          syncForceClientPolicy = context.GetPolicy <SyncForceClientPolicy>();
            var          validMasterCode       = arg.MasterProduct.MasterCode.ProposeValidId();
            var          sellableItemId        = $"{CommerceEntity.IdPrefix<SellableItem>()}{validMasterCode}";
            SellableItem sellableItem          = null;

            if (await _doesEntityExistPipeline.Run(new FindEntityArgument(typeof(SellableItem), sellableItemId),
                                                   context.CommerceContext.PipelineContextOptions))
            {
                sellableItem = await _getSellableItemPipeline.Run(new ProductArgument { CatalogName = "", ProductId = sellableItemId }, context.CommerceContext.PipelineContextOptions);
            }
            else
            {
                var createResult = (await _createSellableItemPipeline.Run(new CreateSellableItemArgument(arg.MasterProduct.MasterCode.ProposeValidId(), arg.MasterProduct.MasterCode, arg.MasterProduct.MasterCode, string.Empty), context.CommerceContext.PipelineContextOptions));
                sellableItem = createResult?.SellableItems?.FirstOrDefault(s => s.Id == sellableItemId);

                Condition.Requires <SellableItem>(sellableItem).IsNotNull($"{this.Name}: The SellableItem could not be created.");
            }

            //Sitecore should have set this to true, issue created #514238
            sellableItem.IsPersisted = true;

            //Add Base properties
            sellableItem.Brand = arg.MasterProduct.Brand.Name;

            //Clear Tags
            sellableItem.Tags.Clear();

            //Add identifiers
            var identifiersComponent = sellableItem.GetComponent <IdentifiersComponent>();

            identifiersComponent.SKU = arg.MasterProduct.MasterCode;
            if (!identifiersComponent.CustomId.Any(i => i.Key.Equals(syncForceClientPolicy.CustomIdentifierKey)))
            {
                identifiersComponent.CustomId.Add(new Models.CustomIdentifier(syncForceClientPolicy.CustomIdentifierKey, arg.MasterProduct.Id.ToString()));
            }
            sellableItem.SetComponent(identifiersComponent);

            sellableItem = (await _persistEntityPipeline.Run(new PersistEntityArgument(sellableItem), context.CommerceContext.PipelineContextOptions))?.Entity as SellableItem ?? sellableItem;

            arg.SellableItem = sellableItem;
            arg.SellableItems?.Add(sellableItem);

            return(arg);
        }
예제 #6
0
        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);
        }