public static Collection ToShopifyModel(this storefrontModel.Catalog.CatalogSearchResult searchResult, storefrontModel.WorkContext workContext) { var result = new Collection(); if (searchResult.Category != null) { result = searchResult.Category.ToShopifyModel(workContext); } if (searchResult.Products != null) { result.Products = new StorefrontPagedList<Product>(searchResult.Products.Select(x => x.ToShopifyModel(workContext)), searchResult.Products, searchResult.Products.GetPageUrl); result.ProductsCount = searchResult.Products.TotalItemCount; result.AllProductsCount = searchResult.Products.TotalItemCount; } if (searchResult.Aggregations != null) { var tags = searchResult.Aggregations .Where(a => a.Items != null) .SelectMany(a => a.Items.Select(item => item.ToShopifyModel(a.Field, a.Label))) .ToList(); result.Tags = new TagCollection(tags); } result.DefaultSortBy = "manual"; if (workContext.CurrentCatalogSearchCriteria != null) { result.SortBy = workContext.CurrentCatalogSearchCriteria.SortBy; } return result; }
public static LineItem ToShopifyModel(this StorefrontModel.Cart.LineItem lineItem, StorefrontModel.WorkContext workContext) { var shopifyModel = new LineItem(); //shopifyModel.Product = lineItem.Product.ToShopifyModel(); shopifyModel.Fulfillment = null; // TODO shopifyModel.Grams = lineItem.Weight; shopifyModel.Id = lineItem.Id; shopifyModel.Image = new Image { Alt = lineItem.Name, Name = lineItem.Name, ProductId = lineItem.ProductId, Src = lineItem.ImageUrl }; shopifyModel.LinePrice = lineItem.ExtendedPrice.Amount * 100; shopifyModel.Price = lineItem.PlacedPrice.Amount * 100; shopifyModel.ProductId = lineItem.ProductId; //shopifyModel.Properties = null; // TODO shopifyModel.Quantity = lineItem.Quantity; shopifyModel.RequiresShipping = lineItem.RequiredShipping; shopifyModel.Sku = lineItem.Sku; shopifyModel.Taxable = lineItem.TaxIncluded; shopifyModel.Title = lineItem.Name; shopifyModel.Type = null; // TODO shopifyModel.Url = null; // TODO shopifyModel.Variant = null; // TODO shopifyModel.VariantId = lineItem.ProductId; shopifyModel.Vendor = null; // TODO return shopifyModel; }
public static Collection ToShopifyModel(this storefrontModel.Catalog.Category category, storefrontModel.WorkContext workContext) { var result = new Collection { Id = category.Id, Description = null, Handle = category.SeoInfo != null ? category.SeoInfo.Slug : category.Id, Title = category.Name, Url = "~/category/" + category.Id }; if(category.PrimaryImage != null) { result.Image = category.PrimaryImage.ToShopifyModel(); } if (category.SeoInfo != null) { result.Url = "~/" + category.SeoInfo.Slug; } if (category.Products != null) { result.Products = new MutablePagedList<Product>((pageNumber, pageSize) => { category.Products.Slice(pageNumber, pageSize); result.ProductsCount = category.Products.TotalItemCount; result.AllProductsCount = category.Products.TotalItemCount; return new StaticPagedList<Product>(category.Products.Select(x => x.ToShopifyModel()), category.Products); }, category.Products.PageNumber, category.Products.PageSize); result.ProductsCount = category.Products.TotalItemCount; result.AllProductsCount = category.Products.TotalItemCount; } if (workContext.Aggregations != null) { result.Tags = new TagCollection(new MutablePagedList<Tag>((pageNumber, pageSize) => { workContext.Aggregations.Slice(pageNumber, pageSize); var tags = workContext.Aggregations.Where(a => a.Items != null) .SelectMany(a => a.Items.Select(item => item.ToShopifyModel(a.Field, a.Label))); return new StaticPagedList<Tag>(tags, workContext.Aggregations); }, workContext.Aggregations.PageNumber, workContext.Aggregations.PageSize)); } result.DefaultSortBy = "manual"; if (workContext.CurrentCatalogSearchCriteria.SortBy != null) { result.SortBy = workContext.CurrentCatalogSearchCriteria.SortBy; } if(!category.Properties.IsNullOrEmpty()) { result.Metafields = new MetaFieldNamespacesCollection(new[] { new MetafieldsCollection("properties", category.Properties) }); } return result; }
public static Cart ToShopifyModel(this StorefrontModel.Cart.ShoppingCart cart, StorefrontModel.WorkContext workContext) { var result = new Cart(); result.Items = cart.Items.Select(x => x.ToShopifyModel(workContext)).ToList(); result.ItemCount = cart.Items.Count(); result.Note = cart.Comment; result.TotalPrice = cart.SubTotal.Amount * 100; result.TotalWeight = cart.Weight; return result; }
public static Customer ToShopifyModel(this CustomerInfo customer, StorefrontModel.WorkContext workContext, StorefrontModel.Common.IStorefrontUrlBuilder urlBuilder) { var result = new Customer(); result.InjectFrom<StorefrontModel.Common.NullableAndEnumValueInjecter>(customer); result.Name = customer.FullName; 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++; } //TODO: make customer.Addresses as IPagedList 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, customer.Orders, customer.Orders.GetPageUrl); } if (customer.QuoteRequests != null) { var quoteRequests = customer.QuoteRequests.Select(qr => qr.ToShopifyModel()).ToList(); result.QuoteRequests = new StorefrontModel.Common.StorefrontPagedList<QuoteRequest>(quoteRequests, customer.QuoteRequests, customer.QuoteRequests.GetPageUrl); } return result; }
public static Shop ToShopifyModel(this StorefrontModel.Store store, StorefrontModel.WorkContext workContext) { Shop result = new Shop(); result.InjectFrom<StorefrontModel.Common.NullableAndEnumValueInjecter>(store); result.CustomerAccountsEnabled = true; result.CustomerAccountsOptional = true; result.Currency = workContext.CurrentCurrency.Code; result.CollectionsCount = "0"; result.Description = store.Description; result.Domain = store.Url; result.Email = store.Email; result.MoneyFormat = ""; result.MoneyWithCurrencyFormat = ""; result.Url = store.Url ?? "~/"; result.Currencies = store.Currencies.Select(x => x.Code).ToArray(); result.Languages = store.Languages.Select(x => x.ToShopifyModel()).ToArray(); result.Catalog = store.Catalog; //result.Metafields = new MetaFieldNamespacesCollection(new[] { new MetafieldsCollection("global", store.DynamicProperties) }); return result; }
public static Variant ToVariant(this StorefrontModel.Catalog.Product product, StorefrontModel.WorkContext workContext) { var result = new Variant(); result.Available = true; //product.IsActive && product.IsBuyable; result.Barcode = product.Gtin; result.CatalogId = product.CatalogId; result.CategoryId = product.CategoryId; result.FeaturedImage = product.PrimaryImage != null ? product.PrimaryImage.ToShopifyModel() : null; if (result.FeaturedImage != null) { result.FeaturedImage.ProductId = product.Id; result.FeaturedImage.AttachedToVariant = true; result.FeaturedImage.Variants = new[] { result }; } result.Id = product.Id; result.InventoryPolicy = "continue"; result.InventoryQuantity = product.Inventory != null ? product.Inventory.InStockQuantity ?? 0 : 0; result.Options = product.VariationProperties.Select(p => p.Value).ToArray(); result.CompareAtPrice = product.Price.ListPrice.Amount * 100; result.Price = product.Price.SalePrice.Amount * 100; if (product.Price.ActiveDiscount != null) { result.Price = result.Price - product.Price.ActiveDiscount.Amount.Amount * 100; } result.Selected = false; result.Sku = product.Sku; result.Title = product.Name; result.Url = "~/product/" + product.Id; if (product.SeoInfo != null) { result.Url = "~/" + product.SeoInfo.Slug; } result.Weight = product.Weight; result.WeightUnit = product.WeightUnit; return result; }
public static Collection ToShopifyModel(this storefrontModel.Catalog.Category category, storefrontModel.WorkContext workContext) { var result = new Collection { Id = category.Id, Description = null, Handle = category.SeoInfo != null ? category.SeoInfo.Slug : category.Id, Title = category.Name, Url = "~/category/" + category.Id }; if(category.PrimaryImage != null) { result.Image = category.PrimaryImage.ToShopifyModel(); } if (category.SeoInfo != null) { result.Url = "~/" + category.SeoInfo.Slug; } return result; }
public static Product ToShopifyModel(this StorefrontModel.Catalog.Product product, StorefrontModel.WorkContext workContext) { var result = new Product(); result.InjectFrom<StorefrontModel.Common.NullableAndEnumValueInjecter>(product); result.Variants.Add(product.ToVariant(workContext)); if (product.Variations != null) { result.Variants.AddRange(product.Variations.Select(x => x.ToVariant(workContext))); } result.Available = true;// product.IsActive && product.IsBuyable; result.CatalogId = product.CatalogId; result.CategoryId = product.CategoryId; result.CompareAtPriceMax = result.Variants.Select(x=>x.CompareAtPrice).Max(); result.CompareAtPriceMin = result.Variants.Select(x => x.CompareAtPrice).Min(); result.CompareAtPriceVaries = result.CompareAtPriceMax != result.CompareAtPriceMin; result.CompareAtPrice = product.Price.ListPrice.Amount * 100; result.Price = product.Price.SalePrice.Amount * 100; if(product.Price.ActiveDiscount != null) { result.Price = result.Price - product.Price.ActiveDiscount.Amount.Amount * 100; } result.PriceMax = result.Variants.Select(x => x.Price).Max(); result.PriceMin = result.Variants.Select(x => x.Price).Min(); result.PriceVaries = result.PriceMax != result.PriceMin; result.Content = product.Description; result.Description = result.Content; result.FeaturedImage = product.PrimaryImage != null ? product.PrimaryImage.ToShopifyModel() : null; if(result.FeaturedImage != null) { result.FeaturedImage.ProductId = product.Id; result.FeaturedImage.AttachedToVariant = false; } result.FirstAvailableVariant = result.Variants.FirstOrDefault(x => x.Available); result.Handle = product.SeoInfo != null ? product.SeoInfo.Slug : product.Id; result.Images = product.Images.Select(x => x.ToShopifyModel()).ToArray(); foreach (var image in result.Images) { image.ProductId = product.Id; image.AttachedToVariant = false; } if(product.VariationProperties != null) { result.Options = product.VariationProperties.Select(x => x.Name).ToArray(); } if(product.Properties != null) { result.Properties = product.Properties.Select(x => x.ToShopifyModel()).ToList(); } result.SelectedVariant = result.Variants.First(); result.Title = product.Name; result.Type = product.ProductType; result.Url = "~/product/" + product.Id; if (product.SeoInfo != null) { result.Url = "~/" + product.SeoInfo.Slug; } return result; }