public void CheckoutQuoteRequest()
        {
            WorkContext          workContext         = GetTestWorkContext();
            IQuoteRequestBuilder quoteRequestBuilder = GetQuoteRequestBuilder();
            var customer = new CustomerInfo
            {
                Id = Guid.NewGuid().ToString(),
                IsRegisteredUser = false
            };

            workContext.CurrentCustomer = customer;

            QuoteRequest quoteRequest = quoteRequestBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, customer, workContext.CurrentLanguage, workContext.CurrentCurrency).Result.QuoteRequest;

            ICatalogSearchService catalogSearchService = GetCatalogSearchService();

            Product[] searchResult = catalogSearchService.GetProductsAsync(new[] { "217be9f3d9064075821f6785dca658b9" }, ItemResponseGroup.ItemLarge).Result;
            quoteRequestBuilder.AddItem(searchResult.First(), 1);
            quoteRequestBuilder.SaveAsync().Wait();

            var quoteItem = quoteRequestBuilder.QuoteRequest.Items.First();

            quoteItem.ProposalPrices.Add(new TierPrice(quoteItem.SalePrice * 0.7, 5));
            quoteItem.ProposalPrices.Add(new TierPrice(quoteItem.SalePrice * 0.5, 10));
            quoteRequestBuilder.SaveAsync().Wait();
            quoteRequest = quoteRequestBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, customer, workContext.CurrentLanguage, workContext.CurrentCurrency).Result.QuoteRequest;

            Assert.NotNull(quoteRequest.Items);
            Assert.Equal(quoteRequest.Items.Count, 1);

            var requestItem = quoteRequest.Items.First();

            foreach (var proposalPrice in requestItem.ProposalPrices)
            {
                requestItem.SelectedTierPrice = proposalPrice;

                ICartBuilder cartBuilder = GetCartBuilder();
                cartBuilder.LoadOrCreateNewTransientCartAsync("default", workContext.CurrentStore, customer, workContext.CurrentLanguage, workContext.CurrentCurrency).Wait();
                cartBuilder.FillFromQuoteRequestAsync(quoteRequest).Wait();
                cartBuilder.SaveAsync().Wait();

                cartBuilder.LoadOrCreateNewTransientCartAsync("default", workContext.CurrentStore, customer, workContext.CurrentLanguage, workContext.CurrentCurrency).Wait();
                var cart = cartBuilder.Cart;
                var item = cart.Items.First();

                Assert.Equal(requestItem.SelectedTierPrice.ActualPrice, item.SalePrice);
                Assert.Equal(requestItem.SelectedTierPrice.Quantity, item.Quantity);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Merge anonymous user quote to newly log in user quote by log in event
        /// </summary>
        public virtual async Task Handle(UserLoginEvent @event)
        {
            if (@event == null)
            {
                return;
            }
            var workContext  = @event.WorkContext;
            var prevUser     = @event.WorkContext.CurrentUser;
            var prevUserCart = @event.WorkContext.CurrentCart.Value;

            //If previous user was anonymous and it has not empty cart need merge anonymous cart to personal
            if (workContext.CurrentStore.QuotesEnabled && !prevUser.IsRegisteredUser &&
                workContext.CurrentQuoteRequest != null && workContext.CurrentQuoteRequest.Value.Items.Any())
            {
                await _quoteBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, @event.User, workContext.CurrentLanguage, workContext.CurrentCurrency);

                await _quoteBuilder.MergeFromOtherAsync(workContext.CurrentQuoteRequest.Value);

                await _quoteBuilder.SaveAsync();
            }
        }
        public override async Task Invoke(IOwinContext context)
        {
            if (IsStorefrontRequest(context.Request))
            {
                var workContext = _container.Resolve <WorkContext>();

                var linkListService      = _container.Resolve <IMenuLinkListService>();
                var cartBuilder          = _container.Resolve <ICartBuilder>();
                var catalogSearchService = _container.Resolve <ICatalogSearchService>();

                // Initialize common properties
                workContext.RequestUrl   = context.Request.Uri;
                workContext.AllCountries = _allCountries;
                workContext.AllStores    = await _cacheManager.GetAsync("GetAllStores", "ApiRegion", async() => await GetAllStoresAsync());

                if (workContext.AllStores != null && workContext.AllStores.Any())
                {
                    // Initialize request specific properties
                    workContext.CurrentStore    = GetStore(context, workContext.AllStores);
                    workContext.CurrentLanguage = GetLanguage(context, workContext.AllStores, workContext.CurrentStore);
                    workContext.AllCurrencies   = await _cacheManager.GetAsync("GetAllCurrencies-" + workContext.CurrentLanguage.CultureName, "ApiRegion", async() => { return((await _commerceApi.CommerceGetAllCurrenciesAsync()).Select(x => x.ToWebModel(workContext.CurrentLanguage)).ToArray()); });

                    //Sync store currencies with avail in system
                    foreach (var store in workContext.AllStores)
                    {
                        store.SyncCurrencies(workContext.AllCurrencies, workContext.CurrentLanguage);
                        store.CurrentSeoInfo = store.SeoInfos.FirstOrDefault(x => x.Language == workContext.CurrentLanguage);
                    }

                    //Set current currency
                    workContext.CurrentCurrency = GetCurrency(context, workContext.CurrentStore);

                    var qs = HttpUtility.ParseQueryString(workContext.RequestUrl.Query);
                    //Initialize catalog search criteria
                    workContext.CurrentCatalogSearchCriteria = new CatalogSearchCriteria(workContext.CurrentLanguage, workContext.CurrentCurrency, qs)
                    {
                        CatalogId = workContext.CurrentStore.Catalog
                    };

                    //This line make delay categories loading initialization (categories can be evaluated on view rendering time)
                    workContext.Categories = new MutablePagedList <Category>((pageNumber, pageSize) =>
                    {
                        var criteria        = workContext.CurrentCatalogSearchCriteria.Clone();
                        criteria.PageNumber = pageNumber;
                        criteria.PageSize   = pageSize;
                        var result          = catalogSearchService.SearchCategories(criteria);
                        foreach (var category in result)
                        {
                            category.Products = new MutablePagedList <Product>((pageNumber2, pageSize2) =>
                            {
                                criteria.CategoryId = category.Id;
                                criteria.PageNumber = pageNumber2;
                                criteria.PageSize   = pageSize2;
                                var searchResult    = catalogSearchService.SearchProducts(criteria);
                                //Because catalog search products returns also aggregations we can use it to populate workContext using C# closure
                                //now workContext.Aggregation will be contains preloaded aggregations for current category
                                workContext.Aggregations = new MutablePagedList <Aggregation>(searchResult.Aggregations);
                                return(searchResult.Products);
                            });
                        }
                        return(result);
                    });
                    //This line make delay products loading initialization (products can be evaluated on view rendering time)
                    workContext.Products = new MutablePagedList <Product>((pageNumber, pageSize) =>
                    {
                        var criteria        = workContext.CurrentCatalogSearchCriteria.Clone();
                        criteria.PageNumber = pageNumber;
                        criteria.PageSize   = pageSize;

                        var result = catalogSearchService.SearchProducts(criteria);
                        //Prevent double api request for get aggregations
                        //Because catalog search products returns also aggregations we can use it to populate workContext using C# closure
                        //now workContext.Aggregation will be contains preloaded aggregations for current search criteria
                        workContext.Aggregations = new MutablePagedList <Aggregation>(result.Aggregations);
                        return(result.Products);
                    });
                    //This line make delay aggregation loading initialization (aggregation can be evaluated on view rendering time)
                    workContext.Aggregations = new MutablePagedList <Aggregation>((pageNumber, pageSize) =>
                    {
                        var criteria        = workContext.CurrentCatalogSearchCriteria.Clone();
                        criteria.PageNumber = pageNumber;
                        criteria.PageSize   = pageSize;
                        //Force to load products and its also populate workContext.Aggregations by preloaded values
                        workContext.Products.Slice(pageNumber, pageSize);
                        return(workContext.Aggregations);
                    });

                    workContext.CurrentOrderSearchCriteria = new Model.Order.OrderSearchCriteria(qs);
                    workContext.CurrentQuoteSearchCriteria = new Model.Quote.QuoteSearchCriteria(qs);

                    //Get current customer
                    workContext.CurrentCustomer = await GetCustomerAsync(context);

                    //Validate that current customer has to store access
                    ValidateUserStoreLogin(context, workContext.CurrentCustomer, workContext.CurrentStore);
                    MaintainAnonymousCustomerCookie(context, workContext);

                    // Gets the collection of external login providers
                    var externalAuthTypes = context.Authentication.GetExternalAuthenticationTypes();

                    workContext.ExternalLoginProviders = externalAuthTypes.Select(at => new LoginProvider
                    {
                        AuthenticationType = at.AuthenticationType,
                        Caption            = at.Caption,
                        Properties         = at.Properties
                    }).ToList();

                    workContext.ApplicationSettings = GetApplicationSettings();

                    //Do not load shopping cart and other for resource requests
                    if (!IsAssetRequest(context.Request))
                    {
                        //Shopping cart
                        await cartBuilder.GetOrCreateNewTransientCartAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                        workContext.CurrentCart = cartBuilder.Cart;

                        if (workContext.CurrentStore.QuotesEnabled)
                        {
                            await _quoteRequestBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                            workContext.CurrentQuoteRequest = _quoteRequestBuilder.QuoteRequest;
                        }

                        var linkLists = await _cacheManager.GetAsync("GetAllStoreLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => await linkListService.LoadAllStoreLinkListsAsync(workContext.CurrentStore.Id));

                        workContext.CurrentLinkLists = linkLists.Where(x => x.Language == workContext.CurrentLanguage).ToList();
                        // load all static content
                        var staticContents = _cacheManager.Get(string.Join(":", "AllStoreStaticContent", workContext.CurrentStore.Id), "ContentRegion", () =>
                        {
                            var allContentItems   = _staticContentService.LoadStoreStaticContent(workContext.CurrentStore).ToList();
                            var blogs             = allContentItems.OfType <Blog>().ToArray();
                            var blogArticlesGroup = allContentItems.OfType <BlogArticle>().GroupBy(x => x.BlogName, x => x).ToList();

                            foreach (var blog in blogs)
                            {
                                var blogArticles = blogArticlesGroup.FirstOrDefault(x => string.Equals(x.Key, blog.Name, StringComparison.OrdinalIgnoreCase));
                                if (blogArticles != null)
                                {
                                    blog.Articles = new MutablePagedList <BlogArticle>(blogArticles);
                                }
                            }

                            return(new { Pages = allContentItems, Blogs = blogs });
                        });
                        workContext.Pages = new MutablePagedList <ContentItem>(staticContents.Pages);
                        workContext.Blogs = new MutablePagedList <Blog>(staticContents.Blogs);

                        // Initialize blogs search criteria
                        workContext.CurrentBlogSearchCritera = new BlogSearchCriteria(qs);

                        //Pricelists
                        var pricelistCacheKey = string.Join("-", "EvaluatePriceLists", workContext.CurrentStore.Id, workContext.CurrentCustomer.Id);
                        workContext.CurrentPricelists = await _cacheManager.GetAsync(pricelistCacheKey, "ApiRegion", async() =>
                        {
                            var evalContext = new VirtoCommerceDomainPricingModelPriceEvaluationContext
                            {
                                StoreId    = workContext.CurrentStore.Id,
                                CatalogId  = workContext.CurrentStore.Catalog,
                                CustomerId = workContext.CurrentCustomer.Id,
                                Quantity   = 1
                            };
                            var pricingResult = await _pricingModuleApi.PricingModuleEvaluatePriceListsAsync(evalContext);
                            return(pricingResult.Select(p => p.ToWebModel()).ToList());
                        });
                    }
                }
            }

            await Next.Invoke(context);
        }
Esempio n. 4
0
        public override async Task Invoke(IOwinContext context)
        {
            if (IsStorefrontRequest(context.Request))
            {
                var workContext = _container.Resolve <WorkContext>();
                var urlBuilder  = _container.Resolve <IStorefrontUrlBuilder>();

                var linkListService = _container.Resolve <IMenuLinkListService>();
                var cartBuilder     = _container.Resolve <ICartBuilder>();

                // Initialize common properties
                workContext.RequestUrl   = context.Request.Uri;
                workContext.AllCountries = _allCountries;
                workContext.AllStores    = await _cacheManager.GetAsync("GetAllStores", "ApiRegion", async() => { return(await GetAllStoresAsync()); });

                if (workContext.AllStores != null && workContext.AllStores.Any())
                {
                    // Initialize request specific properties
                    workContext.CurrentStore    = GetStore(context, workContext.AllStores);
                    workContext.CurrentLanguage = GetLanguage(context, workContext.AllStores, workContext.CurrentStore);
                    workContext.AllCurrencies   = await _cacheManager.GetAsync("GetAllCurrencies-" + workContext.CurrentLanguage.CultureName, "ApiRegion", async() => { return((await _commerceApi.CommerceGetAllCurrenciesAsync()).Select(x => x.ToWebModel(workContext.CurrentLanguage)).ToArray()); });

                    //Sync store currencies with avail in system
                    foreach (var store in workContext.AllStores)
                    {
                        store.SyncCurrencies(workContext.AllCurrencies, workContext.CurrentLanguage);
                        store.CurrentSeoInfo = store.SeoInfos.FirstOrDefault(x => x.Language == workContext.CurrentLanguage);
                    }

                    //Set current currency
                    workContext.CurrentCurrency = GetCurrency(context, workContext.CurrentStore);

                    var qs = HttpUtility.ParseQueryString(workContext.RequestUrl.Query);
                    //Initialize catalog search criteria
                    workContext.CurrentCatalogSearchCriteria           = new CatalogSearchCriteria(qs);
                    workContext.CurrentCatalogSearchCriteria.CatalogId = workContext.CurrentStore.Catalog;
                    workContext.CurrentCatalogSearchCriteria.Currency  = workContext.CurrentCurrency;
                    workContext.CurrentCatalogSearchCriteria.Language  = workContext.CurrentLanguage;

                    workContext.CurrentOrderSearchCriteria = new Model.Order.OrderSearchCriteria(qs);
                    workContext.CurrentQuoteSearchCriteria = new Model.Quote.QuoteSearchCriteria(qs);

                    //Get current customer
                    workContext.CurrentCustomer = await GetCustomerAsync(context);

                    //Validate that current customer has to store access
                    ValidateUserStoreLogin(context, workContext.CurrentCustomer, workContext.CurrentStore);
                    MaintainAnonymousCustomerCookie(context, workContext);

                    //Do not load shopping cart and other for resource requests
                    if (!IsAssetRequest(context.Request))
                    {
                        //Shopping cart
                        await cartBuilder.GetOrCreateNewTransientCartAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                        workContext.CurrentCart = cartBuilder.Cart;

                        if (workContext.CurrentStore.QuotesEnabled)
                        {
                            await _quoteRequestBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                            workContext.CurrentQuoteRequest = _quoteRequestBuilder.QuoteRequest;
                        }

                        var linkLists = await _cacheManager.GetAsync("GetAllStoreLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => await linkListService.LoadAllStoreLinkListsAsync(workContext.CurrentStore.Id));

                        workContext.CurrentLinkLists = linkLists.Where(x => x.Language == workContext.CurrentLanguage).ToList();


                        //Initialize blogs search criteria
                        //TODO: read from query string
                        workContext.CurrentBlogSearchCritera = new Model.StaticContent.BlogSearchCriteria(qs);

                        //Pricelists
                        var pricelistCacheKey = string.Join("-", "EvaluatePriceLists", workContext.CurrentStore.Id, workContext.CurrentCustomer.Id);
                        workContext.CurrentPricelists = await _cacheManager.GetAsync(pricelistCacheKey, "ApiRegion", async() =>
                        {
                            var evalContext = new VirtoCommerceDomainPricingModelPriceEvaluationContext
                            {
                                StoreId    = workContext.CurrentStore.Id,
                                CatalogId  = workContext.CurrentStore.Catalog,
                                CustomerId = workContext.CurrentCustomer.Id,
                                Quantity   = 1
                            };
                            var pricingResult = await _pricingModuleApi.PricingModuleEvaluatePriceListsAsync(evalContext);
                            return(pricingResult.Select(p => p.ToWebModel()).ToList());
                        });
                    }
                }
            }

            await Next.Invoke(context);
        }