Esempio n. 1
0
        /// <summary>
        /// Gets the product price.
        /// </summary>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="productViewModels">The product view models.</param>
        public virtual void GetProductBulkPrices([NotNull] VisitorContext visitorContext, List <ProductViewModel> productViewModels)
        {
            if (productViewModels == null || !productViewModels.Any())
            {
                return;
            }

            var catalogName = productViewModels.Select(p => p.CatalogName).First().ToString();
            var productIds  = productViewModels.Select(p => p.ProductId).ToList();

            var pricesResponse = this.PricingManager.GetProductBulkPrices(StorefrontManager.CurrentStorefront, visitorContext, catalogName, productIds, null);
            var prices         = pricesResponse != null && pricesResponse.Result != null ? pricesResponse.Result : new Dictionary <string, Price>();

            foreach (var productViewModel in productViewModels)
            {
                Price price;
                if (prices.Any() && prices.TryGetValue(productViewModel.ProductId, out price))
                {
                    ExtendedCommercePrice extendedPrice = (ExtendedCommercePrice)price;

                    productViewModel.ListPrice     = extendedPrice.Amount;
                    productViewModel.AdjustedPrice = extendedPrice.ListPrice;

                    productViewModel.LowestPricedVariantAdjustedPrice  = extendedPrice.LowestPricedVariant;
                    productViewModel.LowestPricedVariantListPrice      = extendedPrice.LowestPricedVariantListPrice;
                    productViewModel.HighestPricedVariantAdjustedPrice = extendedPrice.HighestPricedVariant;
                }
            }
        }
        /// <summary>
        /// Gets the product price.
        /// </summary>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="productViewModel">The product view model.</param>
        public virtual void GetProductPrice([NotNull] VisitorContext visitorContext, ProductViewModel productViewModel)
        {
            if (productViewModel == null)
            {
                return;
            }

            var pricesResponse = this.PricingManager.GetProductPrices(StorefrontManager.CurrentStorefront, visitorContext, productViewModel.ProductId, null);

            if (pricesResponse != null && pricesResponse.ServiceProviderResult.Success && pricesResponse.Result != null)
            {
                Price price;
                if (pricesResponse.Result.TryGetValue(productViewModel.ProductId, out price))
                {
                    productViewModel.ListPrice     = price.Amount;
                    productViewModel.AdjustedPrice = ((CommercePrice)price).ListPrice;

                    // The current implementation assumes all variants have the same price as the product.  We need to set the variant
                    // prices so they will render properly.
                    if (productViewModel.Variants.Any())
                    {
                        foreach (var variant in productViewModel.Variants)
                        {
                            variant.ListPrice     = productViewModel.ListPrice;
                            variant.AdjustedPrice = productViewModel.AdjustedPrice;
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the product price.
        /// </summary>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="productViewModel">The product view model.</param>
        public virtual void GetProductPrice([NotNull] VisitorContext visitorContext, ProductViewModel productViewModel)
        {
            if (productViewModel == null)
            {
                return;
            }

            bool includeVariants = productViewModel.Variants != null && productViewModel.Variants.Count > 0;
            var  pricesResponse  = this.PricingManager.GetProductPrices(StorefrontManager.CurrentStorefront, visitorContext, productViewModel.CatalogName, productViewModel.ProductId, includeVariants, null);

            if (pricesResponse != null && pricesResponse.ServiceProviderResult.Success && pricesResponse.Result != null)
            {
                Price price;
                if (pricesResponse.Result.TryGetValue(productViewModel.ProductId, out price))
                {
                    ExtendedCommercePrice extendedPrice = (ExtendedCommercePrice)price;
                    productViewModel.ListPrice     = price.Amount;
                    productViewModel.AdjustedPrice = extendedPrice.ListPrice;
                }

                if (includeVariants)
                {
                    foreach (var variant in productViewModel.Variants)
                    {
                        if (pricesResponse.Result.TryGetValue(variant.VariantId, out price))
                        {
                            ExtendedCommercePrice extendedPrice = (ExtendedCommercePrice)price;

                            variant.ListPrice     = extendedPrice.Amount;
                            variant.AdjustedPrice = extendedPrice.ListPrice;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Logins the specified storefront.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="anonymousVisitorId">The anonymous visitor identifier.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="persistent">if set to <c>true</c> [persistent].</param>
        /// <returns>
        /// True if the user is logged in; Otherwise false.
        /// </returns>
        public virtual bool Login([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] string anonymousVisitorId, string userName, string password, bool persistent)
        {
            Assert.ArgumentNotNullOrEmpty(userName, "userName");
            Assert.ArgumentNotNullOrEmpty(password, "password");

            var anonymousVisitorCart = this.CartManager.GetCurrentCart(storefront, anonymousVisitorId).Result;

            var isLoggedIn = AuthenticationManager.Login(userName, password, persistent);

            if (isLoggedIn)
            {
                Tracker.Current.CheckForNull().Session.Identify(userName);

                visitorContext.SetCommerceUser(this.ResolveCommerceUser().Result);

                this.CartManager.MergeCarts(storefront, visitorContext, anonymousVisitorId, anonymousVisitorCart);
            }

            return(isLoggedIn);
        }
        /// <summary>
        /// Gets the product price.
        /// </summary>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="productViewModel">The product view model.</param>
        public virtual void GetProductPrice([NotNull] VisitorContext visitorContext, ProductViewModel productViewModel)
        {
            if (productViewModel == null)
            {
                return;
            }

            var includeVariants = productViewModel.Variants != null && productViewModel.Variants.Count > 0;
            var pricesResponse  = this.PricingManager.GetProductPrices(StorefrontManager.CurrentStorefront, visitorContext, productViewModel.CatalogName, productViewModel.ProductId, includeVariants, null);

            if (pricesResponse == null || !pricesResponse.ServiceProviderResult.Success || pricesResponse.Result == null)
            {
                return;
            }

            Price price;

            if (pricesResponse.Result.TryGetValue(productViewModel.ProductId, out price))
            {
                var extendedPrice = (Sitecore.Commerce.Engine.Connect.Entities.Prices.ExtendedCommercePrice)price;
                productViewModel.ListPrice     = price.Amount;
                productViewModel.AdjustedPrice = extendedPrice.ListPrice;
            }

            if (!includeVariants)
            {
                return;
            }

            foreach (var variant in productViewModel.Variants)
            {
                if (!pricesResponse.Result.TryGetValue(variant.VariantId, out price))
                {
                    continue;
                }

                var extendedPrice = (Sitecore.Commerce.Engine.Connect.Entities.Prices.ExtendedCommercePrice)price;
                variant.ListPrice     = extendedPrice.Amount;
                variant.AdjustedPrice = extendedPrice.ListPrice;
            }
        }
        /// <summary>
        /// Gets a lists of target items from a relationship field
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="field">the relationship field</param>
        /// <param name="rendering">The target renering.</param>
        /// <returns>
        /// a list of relationship targets or null if no items found
        /// </returns>
        public RelatedCatalogItemsViewModel GetRelationshipsFromField([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, RelationshipField field, Rendering rendering)
        {
            Assert.ArgumentNotNull(storefront, "storefront");

            return(GetRelationshipsFromField(storefront, visitorContext, field, rendering, null));
        }
        /// <summary>
        /// Updates the wish list.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="model">The model.</param>
        /// <returns>
        /// The manager response with the wish list as the result.
        /// </returns>
        public virtual ManagerResponse <UpdateWishListResult, WishList> UpdateWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, UpdateWishListInputModel model)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");

            var request = new UpdateWishListRequest(new WishList {
                UserId = visitorContext.UserId, CustomerId = visitorContext.UserId, ExternalId = model.ExternalId, Name = model.Name, IsFavorite = model.IsFavorite, ShopName = storefront.ShopName
            });
            var result = this.WishListServiceProvider.UpdateWishList(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <UpdateWishListResult, WishList>(result, result.WishList));
        }
        /// <summary>
        /// Adds the lines to wish list.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishListId">The wish list identifier.</param>
        /// <param name="lines">The lines.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <AddLinesToWishListResult, WishList> AddLinesToWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string wishListId, IEnumerable <WishListLine> lines)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(lines, "lines");
            Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId");

            var errorResult = new AddLinesToWishListResult()
            {
                Success = false
            };

            // Limit the number of wish list lines that can get created.
            var wishListResult = this.GetWishList(storefront, visitorContext, wishListId);

            if (!wishListResult.ServiceProviderResult.Success)
            {
                wishListResult.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m));
                return(new ManagerResponse <AddLinesToWishListResult, WishList>(errorResult, null));
            }

            if (wishListResult.Result.Lines.Count() >= StorefrontManager.CurrentStorefront.MaxNumberOfWishListItems)
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.MaxWishListLineLimitReached);
                message = string.Format(CultureInfo.InvariantCulture, message, StorefrontManager.CurrentStorefront.MaxNumberOfWishLists);
                errorResult.SystemMessages.Add(new Commerce.Services.SystemMessage()
                {
                    Message = message
                });
                return(new ManagerResponse <AddLinesToWishListResult, WishList>(errorResult, null));
            }

            var request = new AddLinesToWishListRequest(new WishList {
                UserId = visitorContext.UserId, CustomerId = visitorContext.UserId, ExternalId = wishListId, ShopName = storefront.ShopName
            }, lines);
            var result = this.WishListServiceProvider.AddLinesToWishList(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <AddLinesToWishListResult, WishList>(result, result.WishList));
        }
        /// <summary>
        /// Updates the wish list lines.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishListId">The wish list identifier.</param>
        /// <param name="lines">The lines.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <UpdateWishListLinesResult, WishList> UpdateWishListLines([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string wishListId, IEnumerable <WishListLine> lines)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(lines, "lines");
            Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId");

            var request = new UpdateWishListLinesRequest(new WishList {
                UserId = visitorContext.UserId, CustomerId = visitorContext.UserId, ExternalId = wishListId, ShopName = storefront.ShopName
            }, lines);
            var result = this.WishListServiceProvider.UpdateWishListLines(request);

            if (result.Success && result.WishList != null)
            {
                this.PopulateStockInformation(storefront, result.WishList);
            }
            else if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <UpdateWishListLinesResult, WishList>(result, result.WishList));
        }
Esempio n. 10
0
        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <returns>
        /// The manager response where the success flag is returned in the result.
        /// </returns>
        public virtual ManagerResponse <DeleteUserResult, bool> DeleteUser([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");

            var userName     = visitorContext.UserName;
            var commerceUser = this.GetUser(userName).Result;

            if (commerceUser != null)
            {
                // NOTE: we do not need to call DeleteCustomer because this will delete the commerce server user under the covers
                var request = new DeleteUserRequest(new CommerceUser {
                    UserName = userName
                });
                var result = this.CustomerServiceProvider.DeleteUser(request);

                if (!result.Success)
                {
                    Helpers.LogSystemMessages(result.SystemMessages, result);
                }

                return(new ManagerResponse <DeleteUserResult, bool>(result, result.Success));
            }

            return(new ManagerResponse <DeleteUserResult, bool>(new DeleteUserResult {
                Success = false
            }, false));
        }
        protected IEnumerable <CategoryViewModel> GroupRelationshipsByDescription([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, RelationshipField field, IEnumerable <string> relationshipNames, IEnumerable <CatalogRelationshipInformation> productRelationshipInfoList, Rendering rendering)
        {
            Dictionary <string, CategoryViewModel> relationshipGroups = new Dictionary <string, CategoryViewModel>(StringComparer.OrdinalIgnoreCase);

            if (field != null && productRelationshipInfoList != null)
            {
                foreach (var relationshipInfo in productRelationshipInfoList)
                {
                    if (!relationshipNames.Any() || relationshipNames.Contains(relationshipInfo.RelationshipName, StringComparer.OrdinalIgnoreCase))
                    {
                        Item lookupItem                 = null;
                        bool usingRelationshipName      = string.IsNullOrWhiteSpace(relationshipInfo.RelationshipDescription);
                        var  relationshipDescription    = string.IsNullOrWhiteSpace(relationshipInfo.RelationshipDescription) ? StorefrontManager.GetRelationshipName(relationshipInfo.RelationshipName, out lookupItem) : relationshipInfo.RelationshipDescription;
                        CategoryViewModel categoryModel = null;
                        if (!relationshipGroups.TryGetValue(relationshipDescription, out categoryModel))
                        {
                            categoryModel = new CategoryViewModel
                            {
                                ChildProducts           = new List <ProductViewModel>(),
                                RelationshipName        = relationshipInfo.RelationshipName,
                                RelationshipDescription = relationshipDescription,
                                LookupRelationshipItem  = (usingRelationshipName) ? lookupItem : null
                            };

                            relationshipGroups[relationshipDescription] = categoryModel;
                        }

                        var targetItemId = ID.Parse(relationshipInfo.ToItemExternalId);
                        var targetItem   = field.InnerField.Database.GetItem(targetItemId);
                        var productModel = new ProductViewModel(targetItem);
                        productModel.Initialize(rendering);

                        this.GetProductRating(targetItem);

                        categoryModel.ChildProducts.Add(productModel);
                    }
                }
            }

            if (relationshipGroups.Count > 0)
            {
                List <ProductViewModel> productViewModelList = new List <ProductViewModel>();

                foreach (string key in relationshipGroups.Keys)
                {
                    CategoryViewModel viewModel = relationshipGroups[key];
                    var childProducts           = viewModel.ChildProducts;
                    if (childProducts != null && childProducts.Count > 0)
                    {
                        productViewModelList.AddRange(childProducts);
                    }
                }

                if (productViewModelList.Count > 0)
                {
                    this.GetProductBulkPrices(visitorContext, productViewModelList);
                    this.InventoryManager.GetProductsStockStatusForList(storefront, productViewModelList);
                }
            }

            return(relationshipGroups.Values);
        }
Esempio n. 12
0
        /// <summary>
        /// Sets the primary address.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="addressExternalId">The address external identifier.</param>
        /// <returns>The manager responsed with the success flag in the result.</returns>
        public virtual ManagerResponse <CustomerResult, bool> SetPrimaryAddress([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] string addressExternalId)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNullOrEmpty(addressExternalId, "addressExternalId");

            var userPartiesResponse = this.GetCurrentUserParties(storefront, visitorContext);

            if (userPartiesResponse.ServiceProviderResult.Success)
            {
                var customerResult = new CustomerResult {
                    Success = false
                };
                customerResult.SystemMessages.ToList().AddRange(userPartiesResponse.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <CustomerResult, bool>(customerResult, false));
            }

            var addressesToUpdate = new List <CommerceParty>();

            var notPrimary = userPartiesResponse.Result.SingleOrDefault(address => address.IsPrimary);

            if (notPrimary != null)
            {
                notPrimary.IsPrimary = false;
                addressesToUpdate.Add(notPrimary);
            }

            var primaryAddress = userPartiesResponse.Result.Single(address => address.PartyId == addressExternalId);

            primaryAddress.IsPrimary = true;
            addressesToUpdate.Add(primaryAddress);

            var updatePartiesResponse = this.UpdateParties(storefront, new CommerceCustomer {
                ExternalId = visitorContext.UserId
            }, addressesToUpdate);

            return(new ManagerResponse <CustomerResult, bool>(updatePartiesResponse.ServiceProviderResult, updatePartiesResponse.Result));
        }
Esempio n. 13
0
        /// <summary>
        /// Updates the user password.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>The manager response.</returns>
        public virtual ManagerResponse <UpdatePasswordResult, bool> UpdateUserPassword([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, ChangePasswordInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");
            Assert.ArgumentNotNullOrEmpty(inputModel.OldPassword, "inputModel.OldPassword");
            Assert.ArgumentNotNullOrEmpty(inputModel.NewPassword, "inputModel.NewPassword");

            var userName = visitorContext.UserName;

            var request = new UpdatePasswordRequest(userName, inputModel.OldPassword, inputModel.NewPassword);
            var result  = this.CustomerServiceProvider.UpdatePassword(request);

            if (!result.Success && !result.SystemMessages.Any())
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.PasswordCouldNotBeReset);
                result.SystemMessages.Add(new SystemMessage {
                    Message = message
                });

                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <UpdatePasswordResult, bool>(result, result.Success));
        }
Esempio n. 14
0
        /// <summary>
        /// Removes the parties from current user.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="addressExternalId">The address external identifier.</param>
        /// <returns>
        /// The manager response with the successflag in the Result.
        /// </returns>
        public virtual ManagerResponse <CustomerResult, bool> RemovePartiesFromCurrentUser([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] string addressExternalId)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNullOrEmpty(addressExternalId, "addresseExternalId");

            var getUserResponse = this.GetUser(Context.User.Name);

            if (getUserResponse.Result == null)
            {
                var customerResult = new CustomerResult {
                    Success = false
                };
                customerResult.SystemMessages.ToList().AddRange(getUserResponse.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <CustomerResult, bool>(customerResult, false));
            }

            var customer = new CommerceCustomer {
                ExternalId = getUserResponse.Result.ExternalId
            };
            var parties = new List <CommerceParty> {
                new CommerceParty {
                    ExternalId = addressExternalId
                }
            };

            return(this.RemoveParties(storefront, customer, parties));
        }
Esempio n. 15
0
        /// <summary>
        /// Gets the current user parties.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <returns>The manager response where the list of parties is returned in the response.</returns>
        public virtual ManagerResponse <GetPartiesResult, IEnumerable <CommerceParty> > GetCurrentUserParties([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");

            var result = new GetPartiesResult {
                Success = false
            };
            var getUserResponse = this.GetUser(visitorContext.UserName);

            if (!getUserResponse.ServiceProviderResult.Success || getUserResponse.Result == null)
            {
                result.SystemMessages.ToList().AddRange(getUserResponse.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetPartiesResult, IEnumerable <CommerceParty> >(result, null));
            }

            return(this.GetParties(storefront, new CommerceCustomer {
                ExternalId = getUserResponse.Result.ExternalId
            }));
        }
Esempio n. 16
0
        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>
        /// The manager response where the user is returned.
        /// </returns>
        public virtual ManagerResponse <UpdateUserResult, CommerceUser> UpdateUser([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, ProfileModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");

            UpdateUserResult result;

            var userName     = visitorContext.UserName;
            var commerceUser = this.GetUser(userName).Result;

            if (commerceUser != null)
            {
                commerceUser.FirstName = inputModel.FirstName;
                commerceUser.LastName  = inputModel.LastName;
                commerceUser.Email     = inputModel.Email;
                commerceUser.SetPropertyValue("Phone", inputModel.TelephoneNumber);

                try
                {
                    var request = new UpdateUserRequest(commerceUser);
                    result = this.CustomerServiceProvider.UpdateUser(request);
                }
                catch (Exception ex)
                {
                    result = new UpdateUserResult {
                        Success = false
                    };
                    result.SystemMessages.Add(new Sitecore.Commerce.Services.SystemMessage {
                        Message = ex.Message + "/" + ex.StackTrace
                    });
                }
            }
            else
            {
                // user is authenticated, but not in the CommerceUsers domain - probably here because we are in edit or preview mode
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UpdateUserProfileError);
                message = string.Format(CultureInfo.InvariantCulture, message, Context.User.LocalName);
                result  = new UpdateUserResult {
                    Success = false
                };

                result.SystemMessages.Add(new Commerce.Services.SystemMessage {
                    Message = message
                });
            }

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <UpdateUserResult, CommerceUser>(result, result.CommerceUser));
        }
        /// <summary>
        /// Gets a lists of target items from a relationship field
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="field">the relationship field</param>
        /// <param name="rendering">The target renering.</param>
        /// <param name="relationshipNames">the names of the relationships, to retrieve (for example upsell).</param>
        /// <returns>
        /// a list of relationship targets or null if no items found
        /// </returns>
        public RelatedCatalogItemsViewModel GetRelationshipsFromField([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, RelationshipField field, Rendering rendering, IEnumerable <string> relationshipNames)
        {
            Assert.ArgumentNotNull(storefront, "storefront");

            relationshipNames = relationshipNames ?? Enumerable.Empty <string>();
            relationshipNames = relationshipNames.Select(s => s.Trim());
            var model = new RelatedCatalogItemsViewModel();

            if (field != null)
            {
                var productRelationshipInfoList = field.GetRelationships();
                productRelationshipInfoList = productRelationshipInfoList.OrderBy(x => x.Rank);
                var productModelList = this.GroupRelationshipsByDescription(storefront, visitorContext, field, relationshipNames, productRelationshipInfoList, rendering);
                model.RelatedProducts.AddRange(productModelList);
            }

            model.Initialize(rendering);

            return(model);
        }
        /// <summary>
        /// Gets the wish list.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishListId">The wish list identifier.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <GetWishListResult, WishList> GetWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string wishListId)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId");

            var request = new GetWishListRequest(visitorContext.UserId, wishListId, storefront.ShopName);
            var result  = this.WishListServiceProvider.GetWishList(request);

            if (result.Success && result.WishList != null)
            {
                this.PopulateStockInformation(storefront, result.WishList);
                this.PopulatePriceInformation(storefront, visitorContext, result.WishList);
            }
            else if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <GetWishListResult, WishList>(result, result.WishList));
        }
        /// <summary>
        /// Deletes the wish list.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishListId">The wish list identifier.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <DeleteWishListResult, WishList> DeleteWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string wishListId)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId");

            var request = new DeleteWishListRequest(new WishList {
                UserId = visitorContext.UserId, CustomerId = visitorContext.UserId, ExternalId = wishListId, ShopName = storefront.ShopName
            });
            var result = this.WishListServiceProvider.DeleteWishList(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <DeleteWishListResult, WishList>(result, result.WishList));
        }
        /// <summary>
        /// Creates the wish list.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishListName">Name of the wish list.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <CreateWishListResult, WishList> CreateWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] string wishListName)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNullOrEmpty(wishListName, "wishListName");

            CreateWishListResult errorResult = new CreateWishListResult()
            {
                Success = false
            };

            // Limit the number of wish list that can get created.
            var wishListResponse = this.GetWishLists(storefront, visitorContext);

            if (!wishListResponse.ServiceProviderResult.Success)
            {
                wishListResponse.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m));
                return(new ManagerResponse <CreateWishListResult, WishList>(errorResult, null));
            }

            if (wishListResponse.Result.Count() >= StorefrontManager.CurrentStorefront.MaxNumberOfWishLists)
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.MaxWishListLimitReached);
                message = string.Format(CultureInfo.InvariantCulture, message, StorefrontManager.CurrentStorefront.MaxNumberOfWishLists);
                errorResult.SystemMessages.Add(new Commerce.Services.SystemMessage()
                {
                    Message = message
                });
                return(new ManagerResponse <CreateWishListResult, WishList>(errorResult, null));
            }

            var request = new CreateWishListRequest(visitorContext.UserId, wishListName, storefront.ShopName);
            var result  = this.WishListServiceProvider.CreateWishList(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <CreateWishListResult, WishList>(result, result.WishList));
        }
Esempio n. 21
0
        /// <summary>
        /// Gets the shipping methods.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>
        /// The manager response where the shipping methods are returned in the Result.
        /// </returns>
        public ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> > GetShippingMethods([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] GetShippingMethodsInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");

            GetShippingMethodsResult result = new GetShippingMethodsResult {
                Success = false
            };
            var cartResult = this.CartManager.GetCurrentCart(storefront, visitorContext);

            if (!cartResult.ServiceProviderResult.Success || cartResult.Result == null)
            {
                result.SystemMessages.ToList().AddRange(cartResult.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, null));
            }

            var cart           = cartResult.Result;
            var preferenceType = InputModelExtension.GetShippingOptionType(inputModel.ShippingPreferenceType);

            var request = new Sitecore.Commerce.Engine.Connect.Services.Shipping.GetShippingMethodsRequest(
                new ShippingOption {
                ShippingOptionType = preferenceType
            },
                (inputModel.ShippingAddress != null) ? inputModel.ShippingAddress.ToParty() : null,
                cart)
            {
                Lines = (inputModel.Lines != null) ? inputModel.Lines.ToCommerceCartLines() : null
            };

            result = this.ShippingServiceProvider.GetShippingMethods <GetShippingMethodsRequest, GetShippingMethodsResult>(request);
            return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, result.ShippingMethods));
        }
        /// <summary>
        /// Gets the wish lists.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <GetWishListsResult, IEnumerable <WishListHeader> > GetWishLists([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");

            var request = new GetWishListsRequest(visitorContext.UserId, storefront.ShopName);
            var result  = this.WishListServiceProvider.GetWishLists(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <GetWishListsResult, IEnumerable <WishListHeader> >(result, result.WishLists.ToList()));
        }
        /// <summary>
        /// Gets the gift card balance.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="giftCardId">The gift card identifier.</param>
        /// <returns>
        /// The manager response where the gift card balance is returned in the Result.
        /// </returns>
        public ManagerResponse <GetGiftCardResult, decimal> GetGiftCardBalance([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] string giftCardId)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNullOrEmpty(giftCardId, "giftCardId");

            var result = this.GetGiftCard(giftCardId, storefront.ShopName).ServiceProviderResult;

            Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <GetGiftCardResult, decimal>(result, (result.Success && result.GiftCard != null) ? result.GiftCard.Balance : -1));
        }
        /// <summary>
        /// Removes the wish list lines.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishListId">The wish list identifier.</param>
        /// <param name="models">The models.</param>
        /// <returns>The manager response with the wish list as the result.</returns>
        public virtual ManagerResponse <RemoveWishListLinesResult, WishList> RemoveWishListLines([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string wishListId, IEnumerable <WishListLineInputModel> models)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(models, "models");
            Assert.ArgumentNotNullOrEmpty(wishListId, "wishListId");

            var productIds = models.Select(model => string.IsNullOrEmpty(model.VariantId) ? model.ProductId : model.VariantId).ToList();
            var request    = new RemoveWishListLinesRequest(new WishList {
                UserId = visitorContext.UserId, CustomerId = visitorContext.UserId, ExternalId = wishListId, ShopName = storefront.ShopName
            }, productIds);
            var result = this.WishListServiceProvider.RemoveWishListLines(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <RemoveWishListLinesResult, WishList>(result, result.WishList));
        }
Esempio n. 25
0
        /// <summary>
        /// Sets the shipping addresses.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="cart">The cart.</param>
        /// <param name="shippingAddresses">The shipping addresses.</param>
        /// <param name="emailPartyList">The email party list.</param>
        /// <returns>
        /// The manager response with a cart in the result.
        /// </returns>
        protected virtual AddPartiesResult SetShippingAddresses(CommerceStorefront storefront, VisitorContext visitorContext, CommerceCart cart, List<PartyInputModelItem> shippingAddresses, List<RefSFModels.EmailParty> emailPartyList)
        {
            var errorResult = new AddPartiesResult { Success = false };

            var removePartiesResponse = this.RemoveAllShippingParties(storefront, visitorContext, cart);
            if (!removePartiesResponse.ServiceProviderResult.Success)
            {
                errorResult.SystemMessages.Add(removePartiesResponse.ServiceProviderResult.SystemMessages[0]);
                return errorResult;
            }

            List<Party> partyList = new List<Party>();

            if (shippingAddresses != null && shippingAddresses.Any())
            {
                partyList.AddRange(shippingAddresses.ToNewShippingParties());
            }

            if (emailPartyList != null && emailPartyList.Any())
            {
                partyList.AddRange(emailPartyList.Cast<Party>().ToList());
            }

            AddPartiesRequest addPartiesRequest = new AddPartiesRequest(cart, partyList);
            var addPartiesResponse = this.CartServiceProvider.AddParties(addPartiesRequest);

            return addPartiesResponse;
        }
        /// <summary>
        /// Updates a single wish list line.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="model">The model.</param>
        /// <returns>The manager response with the updated wish list as the result.</returns>
        public virtual ManagerResponse <UpdateWishListLinesResult, WishList> UpdateWishListLine([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] WishListLineInputModel model)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(model, "model");
            Assert.ArgumentNotNullOrEmpty(model.WishListId, "model.WishListId");
            Assert.ArgumentNotNullOrEmpty(model.ProductId, "model.ProductId");

            var wishListLine = new WishListLine
            {
                Product = new CommerceCartProduct {
                    ProductId = model.ProductId, ProductVariantId = model.VariantId
                },
                Quantity = model.Quantity
            };

            return(this.UpdateWishListLines(storefront, visitorContext, model.WishListId, new List <WishListLine> {
                wishListLine
            }));
        }
Esempio n. 27
0
        /// <summary>
        /// Gets the product bulk prices.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="catalogName">Name of the catalog.</param>
        /// <param name="productIds">The product ids.</param>
        /// <returns>
        /// The manager response with the list of prices in the Result.
        /// </returns>
        public virtual ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Price> > GetProductBulkPrices([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string catalogName, IEnumerable <string> productIds)
        {
            Assert.ArgumentNotNull(storefront, "storefront");

            var request = new Sitecore.Reference.Storefront.Connect.Pipelines.Arguments.GetProductBulkPricesRequest(catalogName, productIds);

            request.CurrencyCode = StorefrontManager.GetCustomerCurrency();

            var result = this.PricingServiceProvider.GetProductBulkPrices(request);

            Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Price> >(result, result.Prices ?? new Dictionary <string, Price>()));
        }
        /// <summary>
        /// Adds the lines to wish list.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="model">The model.</param>
        /// <returns>
        /// The manager response with the wish list as the result.
        /// </returns>
        public virtual ManagerResponse <AddLinesToWishListResult, WishList> AddLinesToWishList([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, AddToWishListInputModel model)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(model, "model");

            var product = new CommerceCartProduct
            {
                ProductCatalog   = model.ProductCatalog,
                ProductId        = model.ProductId,
                ProductVariantId = model.VariantId
            };

            var line = new WishListLine
            {
                Product  = product,
                Quantity = model.Quantity == null ? 1 : (uint)model.Quantity
            };

            if (line.Product.ProductId.Equals(storefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase))
            {
                line.Properties.Add("GiftCardAmount", model.GiftCardAmount);
            }

            // create wish list
            if (model.WishListId == null && !string.IsNullOrEmpty(model.WishListName))
            {
                var newList = this.CreateWishList(storefront, visitorContext, model.WishListName).Result;
                if (newList == null)
                {
                    return(new ManagerResponse <AddLinesToWishListResult, WishList>(new AddLinesToWishListResult {
                        Success = false
                    }, null));
                }

                model.WishListId = newList.ExternalId;
            }

            var result = this.AddLinesToWishList(storefront, visitorContext, model.WishListId, new List <WishListLine> {
                line
            });

            return(new ManagerResponse <AddLinesToWishListResult, WishList>(result.ServiceProviderResult, result.ServiceProviderResult.WishList));
        }
Esempio n. 29
0
        /// <summary>
        /// Gets the product prices.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="productId">The product identifier.</param>
        /// <param name="priceTypeIds">The price type ids.</param>
        /// <returns>
        /// The manager response with the list of prices in the Result.
        /// </returns>
        public virtual ManagerResponse <GetProductPricesResult, IDictionary <string, Price> > GetProductPrices([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, string productId, params string[] priceTypeIds)
        {
            Assert.ArgumentNotNull(storefront, "storefront");

            var request = new GetProductPricesRequest(productId, priceTypeIds);

            if (Sitecore.Context.User.IsAuthenticated)
            {
                request.UserId = visitorContext.GetCustomerId();
            }

            request.CurrencyCode = StorefrontManager.GetCustomerCurrency();

            var result = this.PricingServiceProvider.GetProductPrices(request);

            Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <GetProductPricesResult, IDictionary <string, Price> >(result, result.Prices ?? new Dictionary <string, Price>()));
        }
        /// <summary>
        /// Populates the price information.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="wishList">The wish list.</param>
        protected virtual void PopulatePriceInformation(CommerceStorefront storefront, VisitorContext visitorContext, WishList wishList)
        {
            if (wishList.Lines != null && wishList.Lines.Any())
            {
                string catalogName    = ((CommerceCartProduct)wishList.Lines[0].Product).ProductCatalog;
                var    productIds     = wishList.Lines.Select(l => l.Product.ProductId).ToArray();
                var    pricesResponse = this.PricingManager.GetProductBulkPrices(StorefrontManager.CurrentStorefront, visitorContext, catalogName, productIds);

                var prices = pricesResponse != null && pricesResponse.ServiceProviderResult.Success && pricesResponse.Result != null ? pricesResponse.Result : new Dictionary <string, Price>();

                foreach (var line in wishList.Lines)
                {
                    Price price;
                    if (prices.Any() && prices.TryGetValue(line.Product.ProductId, out price))
                    {
                        line.Product.Price.Amount = ((CommercePrice)price).ListPrice;
                    }
                }
            }
        }
        public RelatedCatalogItemsViewModel GetRelationshipsFromItem([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, Item catalogItem, Rendering rendering)
        {
            Assert.ArgumentNotNull(storefront, "storefront");

            if (catalogItem != null &&
                catalogItem.Fields.Contains(CommerceConstants.KnownFieldIds.RelationshipList) &&
                !string.IsNullOrEmpty(catalogItem[CommerceConstants.KnownFieldIds.RelationshipList]))
            {
                var field = new RelationshipField(catalogItem.Fields[CommerceConstants.KnownFieldIds.RelationshipList]);
                if (rendering != null &&
                    !string.IsNullOrWhiteSpace(rendering.RenderingItem.InnerItem["RelationshipsToDisplay"]))
                {
                    var relationshipsToDisplay = rendering.RenderingItem.InnerItem["RelationshipsToDisplay"].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    return(this.GetRelationshipsFromField(storefront, visitorContext, field, rendering, relationshipsToDisplay));
                }
                else
                {
                    return(this.GetRelationshipsFromField(storefront, visitorContext, field, rendering));
                }
            }

            return(null);
        }