Пример #1
0
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        private void TryGetCustomer(Guid key)
        {
            var customer = (ICustomerBase)_cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            var isLoggedIn = (bool)_cache.RequestCache.GetCacheItem(CacheKeys.CustomerIsLoggedIn(key), () => _membershipHelper.IsLoggedIn());

            // check the cache for a previously retrieved customer
            if (customer != null)
            {
                CurrentCustomer = customer;

                if (customer.IsAnonymous)
                {
                    if (isLoggedIn)
                    {
                        var memberId = _membershipHelper.GetCurrentMemberId();
                        var member   = _memberService.GetById(memberId);

                        if (MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
                        {
                            var anonymousBasket = Basket.GetBasket(_merchelloContext, customer);

                            customer = _customerService.GetByLoginName(member.Username) ??
                                       _customerService.CreateCustomerWithKey(member.Username);


                            ContextData.Key = customer.Key;
                            ContextData.Values.Add(new KeyValuePair <string, string>(UmbracoMemberIdDataKey, memberId.ToString(CultureInfo.InvariantCulture)));
                            var customerBasket = Basket.GetBasket(_merchelloContext, customer);

                            //// convert the customer basket
                            ConvertBasket(anonymousBasket, customerBasket);

                            CacheCustomer(customer);
                            CurrentCustomer = customer;

                            return;
                        }
                    }
                }
                else if (customer.IsAnonymous == false && isLoggedIn == false)
                {
                    CreateAnonymousCustomer();
                    return;
                }
                else if (customer.IsAnonymous == false && isLoggedIn)
                {
                    this.EnsureIsLoggedInCustomer(customer);
                }

                ContextData.Key = customer.Key;

                return;
            }

            // try to get the customer
            customer = _customerService.GetAnyByKey(key);

            if (customer != null)
            {
                CurrentCustomer = customer;
                ContextData.Key = customer.Key;
                if (isLoggedIn)
                {
                    ContextData.Values.Add(new KeyValuePair <string, string>(UmbracoMemberIdDataKey, _membershipHelper.GetCurrentMemberId().ToString(CultureInfo.InvariantCulture)));
                }
                CacheCustomer(customer);
            }
            else
            {
                // create a new anonymous customer
                CreateAnonymousCustomer();
            }
        }
Пример #2
0
 /// <summary>
 /// Wrapper to cache the logged in status in the request cache
 /// </summary>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected bool IsLoggedIn(Guid key)
 {
     return((bool)Cache.RequestCache.GetCacheItem(CacheKeys.CustomerIsLoggedIn(key), () => this.GetIsCurrentlyLoggedIn()));
 }
Пример #3
0
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        private void TryGetCustomer(Guid key)
        {
            var customer = (ICustomerBase)_cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            var isLoggedIn = (bool)_cache.RequestCache.GetCacheItem(CacheKeys.CustomerIsLoggedIn(key), () => _membershipHelper.IsLoggedIn());


            // check the cache for a previously retrieved customer
            if (customer != null)
            {
                CurrentCustomer = customer;

                if (customer.IsAnonymous)
                {
                    if (isLoggedIn)
                    {
                        var memberId = _membershipHelper.GetCurrentMemberId();
                        var member   = _memberService.GetById(memberId);

                        if (MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
                        {
                            var anonymousBasket = Basket.GetBasket(_merchelloContext, customer);

                            customer = _customerService.GetByLoginName(member.Username) ??
                                       _customerService.CreateCustomerWithKey(member.Username);


                            ContextData.Key = customer.Key;

                            var customerBasket = Basket.GetBasket(_merchelloContext, customer);

                            //// convert the customer basket
                            ConvertBasket(anonymousBasket, customerBasket);

                            CacheCustomer(customer);
                            CurrentCustomer = customer;

                            return;
                        }
                    }
                }
                else if (customer.IsAnonymous == false && isLoggedIn == false)
                {
                    // customer has logged out, so we need to go back to an anonymous customer
                    var cookie = _umbracoContext.HttpContext.Request.Cookies[CustomerCookieName];

                    cookie.Expires = DateTime.Now.AddDays(-1);

                    _cache.RequestCache.ClearCacheItem(CustomerCookieName);
                    _cache.RuntimeCache.ClearCacheItem(CacheKeys.CustomerCacheKey(customer.Key));

                    Initialize();

                    return;
                }

                ContextData.Key = customer.Key;

                return;
            }

            // try to get the customer
            customer = _customerService.GetAnyByKey(key);

            if (customer != null)
            {
                CurrentCustomer = customer;
                ContextData.Key = customer.Key;
                CacheCustomer(customer);
            }
            else
            {
                // create a new anonymous customer
                CreateAnonymousCustomer();
            }
        }