Пример #1
0
        public async Task<CartBuilder> GetOrCreateNewTransientCartAsync(Store store, Customer customer, Language language, Currency currency)
        {
            _store = store;
            _customer = customer;
            _currency = currency;
            _language = language;
            _cartCacheKey = GetCartCacheKey(store.Id, customer.Id);

            _cart = await _cacheManager.GetAsync(_cartCacheKey, _cartCacheRegion, async () =>
            {
                ShoppingCart retVal;

                var cartSearchResult = await _cartApi.CartModuleGetCurrentCartAsync(_store.Id, _customer.Id);
                if (cartSearchResult == null)
                {
                    retVal = CreateNewTransientCart();
                }
                else
                {
                    var detalizedCart = await _cartApi.CartModuleGetCartByIdAsync(cartSearchResult.Id);
                    retVal = detalizedCart.ToWebModel(_currency, _language);
                }

                return retVal;
            });

            await EvaluatePromotionsAsync();

            return this;
        }
Пример #2
0
        public static Customer ToShopifyModel(this StorefrontModel.Customer customer, StorefrontModel.WorkContext workContext, StorefrontModel.Common.IStorefrontUrlBuilder urlBuilder)
        {
            var result = new Customer();

            result.InjectFrom <StorefrontModel.Common.NullableAndEnumValueInjecter>(customer);

            result.DefaultAddress         = customer.DefaultAddress.ToShopifyModel();
            result.DefaultBillingAddress  = customer.DefaultBillingAddress.ToShopifyModel();
            result.DefaultShippingAddress = customer.DefaultShippingAddress.ToShopifyModel();

            if (customer.Tags != null)
            {
                result.Tags = customer.Tags.ToList();
            }

            if (customer.Addresses != null)
            {
                var addresses = customer.Addresses.Select(a => a.ToShopifyModel()).ToList();

                // Add virtual ID to each address
                var id = 1;
                foreach (var address in addresses)
                {
                    address.Id = id.ToString(CultureInfo.InvariantCulture);
                    id++;
                }

                result.Addresses = new StorefrontModel.Common.StorefrontPagedList <Address>(addresses, 1, 10, addresses.Count, page => workContext.RequestUrl.SetQueryParameter("page", page.ToString()).ToString());
            }

            if (customer.Orders != null)
            {
                var orders = customer.Orders.Select(o => o.ToShopifyModel(urlBuilder)).ToList();
                result.Orders = new StorefrontModel.Common.StorefrontPagedList <Order>(orders, 1, 10, customer.OrdersCount, page => workContext.RequestUrl.SetQueryParameter("page", page.ToString()).ToString());
            }

            return(result);
        }
Пример #3
0
        public static Customer ToWebModel(this VirtoCommerceCustomerModuleWebModelContact contact, string userName)
        {
            var customer = new Customer();
            customer.InjectFrom(contact);
            customer.UserName = userName;

            if (contact.Addresses != null)
            {
                customer.Addresses = contact.Addresses.Select(a => a.ToWebModel()).ToList();
            }

            customer.DefaultBillingAddress = customer.Addresses.FirstOrDefault(a => (a.Type & AddressType.Billing) == AddressType.Billing);
            customer.DefaultShippingAddress = customer.Addresses.FirstOrDefault(a => (a.Type & AddressType.Shipping) == AddressType.Shipping);

            // TODO: Need separate properties for first, middle and last name
            if (!string.IsNullOrEmpty(contact.FullName))
            {
                var nameParts = contact.FullName.Split(_nameSeparator, 2);

                if (nameParts.Length > 0)
                {
                    customer.FirstName = nameParts[0];
                }

                if (nameParts.Length > 1)
                {
                    customer.LastName = nameParts[1];
                }
            }

            if (contact.Emails != null)
            {
                customer.Email = contact.Emails.FirstOrDefault();
            }

            return customer;
        }
        protected virtual async Task<Customer> GetCustomerAsync(IOwinContext context)
        {
            var customer = new Customer();

            if (context.Authentication.User.Identity.IsAuthenticated)
            {
                var user = await _platformApi.SecurityGetUserByNameAsync(context.Authentication.User.Identity.Name);
                if (user != null)
                {
                    var contact = await _customerApi.CustomerModuleGetContactByIdAsync(user.Id);
                    if (contact != null)
                    {
                        customer = contact.ToWebModel(user.UserName);
                        customer.HasAccount = true;
                    }
                }
            }

            if (!customer.HasAccount)
            {
                customer.Id = context.Request.Cookies[StorefrontConstants.AnonymousCustomerIdCookie];
                customer.UserName = StorefrontConstants.AnonymousUsername;
                customer.Name = StorefrontConstants.AnonymousUsername;
            }

            return customer;
        }