public static Collection AsWebModel(this Data.Category category)
        {
            var collection = new Collection();

            var urlTemplate = VirtualPathUtility.ToAbsolute("~/collections/{0}");

            collection.AllTypes      = null; // TODO
            collection.AllVendors    = null; // TODO
            collection.CurrentType   = null; // TODO
            collection.CurrentVendor = null; // TODO
            collection.DefaultSortBy = "manual";
            collection.Description   = null; // TODO
            collection.Handle        = category.Code;
            collection.Id            = category.Id;

            if (category.Image != null)
            {
                collection.Image = category.Image.AsWebModel(category.Image.Name, category.Id);
            }

            collection.Keywords = category.Seo != null?category.Seo.Select(k => k.AsWebModel()) : null;

            collection.NextProduct = null; // TODO
            collection.Parents     = category.Parents != null?category.Parents.Select(p => p.AsWebModel()) : null;

            collection.PreviousProduct = null; // TODO
            collection.TemplateSuffix  = null; // TODO
            collection.Title           = category.Name;
            collection.Url             = string.Format(urlTemplate, category.Code);

            // specify SEO based url
            var outline = collection.BuildOutline(Thread.CurrentThread.CurrentUICulture.Name).Select(x => x.Value);

            if (outline.Any())
            {
                var urlHelper = UrlHelperExtensions.GetUrlHelper();
                collection.Outline = string.Join("/", outline);
                collection.Url     = urlHelper.CategoryUrl(collection.Outline);
            }

            return(collection);
        }
Exemple #2
0
        public static Product AsWebModel(
            this Data.Product product, IEnumerable <Data.Price> prices,
            IEnumerable <Data.Marketing.PromotionReward> rewards, Collection collection = null)
        {
            var productModel = new Product();

            var allImages = product.Images.Concat(new[] { product.PrimaryImage });

            var pathTemplate = VirtualPathUtility.ToAbsolute("~/products/{0}");
            var description  = product.EditorialReviews != null?
                               product.EditorialReviews.FirstOrDefault(er => er.ReviewType != null && er.ReviewType.Equals("quickreview", StringComparison.OrdinalIgnoreCase)) : null;

            var fieldsCollection = new MetafieldsCollection("global", product.Properties);
            var options          = GetOptions(product.VariationProperties);

            var keywords = product.Seo != null?product.Seo.Select(k => k.AsWebModel()) : null;

            var primaryImage = product.PrimaryImage ?? (product.Images != null ? product.Images.FirstOrDefault() : null);

            productModel.Description = description != null ? description.Content : null;
            productModel.Handle      = product.Code;
            productModel.Id          = product.Id;
            productModel.Images      = product.Images != null ?
                                       new ItemCollection <Image>(allImages.Select(i => i.AsWebModel(product.Name, product.Id))) : null;
            productModel.IsQuotable    = true; // TODO
            productModel.FeaturedImage = primaryImage != null?
                                         primaryImage.AsWebModel(primaryImage.Name, product.Id) : null;

            productModel.Keywords       = keywords;
            productModel.Metafields     = new MetaFieldNamespacesCollection(new[] { fieldsCollection });
            productModel.Options        = options;
            productModel.Tags           = null; // TODO
            productModel.TemplateSuffix = null; // TODO
            productModel.Title          = product.Name;
            productModel.Type           = product.ProductType;
            productModel.Url            = string.Format(pathTemplate, product.Code);
            productModel.Vendor         = fieldsCollection.ContainsKey("brand") ? fieldsCollection["brand"] as string : null;
            productModel.TaxType        = product.TaxType;

            if (collection != null)
            {
                productModel.Collections = new Collections(new[] { collection });
            }

            // form url
            // "/products/code" or "/en-us/store/collection/outline"
            // specify SEO based url
            var urlHelper = UrlHelperExtensions.GetUrlHelper();
            var url       = String.Empty;

            if (urlHelper != null && collection != null && productModel.Keywords != null && productModel.Keywords.Any())
            {
                var keyword = productModel.Keywords.SeoKeyword(Thread.CurrentThread.CurrentUICulture.Name);
                if (keyword != null)
                {
                    url = urlHelper.ItemUrl(keyword.Keyword, collection == null ? "" : collection.Outline);
                    if (!String.IsNullOrEmpty(url))
                    {
                        productModel.Url = url;
                    }
                }
            }

            if (String.IsNullOrEmpty(url) && urlHelper != null && collection != null)
            {
                url = urlHelper.ItemUrl(productModel.Handle, collection == null ? "" : collection.Outline);
                if (!String.IsNullOrEmpty(url))
                {
                    productModel.Url = url;
                }
            }

            var productRewards = rewards.Where(r => r.RewardType == "CatalogItemAmountReward" && r.ProductId == product.Id && r.IsValid);

            if (product.Variations != null)
            {
                foreach (var variation in product.Variations)
                {
                    var price = prices.FirstOrDefault(p => p.ProductId == variation.Id);

                    productModel.Variants.Add(variation.AsVariantWebModel(price, options, productRewards));
                }
            }

            var productPrice = prices.FirstOrDefault(p => p.ProductId == product.Id);

            var variant = product.AsVariantWebModel(productPrice, options, productRewards);

            variant.Title = "Default Title";

            productModel.Variants.Add(variant);

            return(productModel);
        }