Example #1
0
        private void BuildGoogleFeed()
        {
            //
            // the majority of the code to follow is based upon the excellent blog below:
            // http://blog.codenamed.nl/2015/05/14/creating-a-google-shopping-feed-with-c/
            //

            // get an instance of the store application
            var HccApp = HotcakesApplication.Current;

            var totalProducts = 0;
            var feed          = new FeedInfo();

            // get the store URL based upon SSL
            var storeUrl = Request.IsSecureConnection ? HccApp.CurrentStore.RootUrlSecure() : HccApp.CurrentStore.RootUrl();
            // used to get currency code
            var regionInfo = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);

            feed.Link    = storeUrl;
            feed.Title   = HccApp.CurrentStore.StoreName;
            feed.Updated = DateTime.Now;
            feed.Entries = new List <EntryInfo>();

            // find all products in the store that are active
            var searchCriteria = new ProductSearchCriteria
            {
                DisplayInactiveProducts = false
            };

            // using the search instead of querying the Products directly to use cache
            var products = HccApp.CatalogServices.Products.FindByCriteria(searchCriteria, 1, int.MaxValue, ref totalProducts);

            // non-shipping
            var shippingInfo = new ShippingInfo {
                Price = Constants.HARDCODED_PRICE_ZERO
            };
            // not taxable (e.g., software)
            var taxInfo = new TaxInfo {
                Rate = Constants.HARDCODED_PRICE_ZERO
            };

            // iterate through each product and add it to the feed
            foreach (var product in products)
            {
                var productUrl = UrlRewriter.BuildUrlForProduct(product);

                var productEntry = new EntryInfo
                {
                    Availablity           = GetStockMessage(product),            // OPTIONS: preorder, in stock, out of stock
                    Condition             = Constants.CONDITION_NEW,             // OPTIONS: new, refurbished, used
                    Description           = product.LongDescription,
                    GoogleProductCategory = Constants.HARDCODED_GOOGLE_CATEGORY, // hard-coded for this example project (see property attributes)
                    Id          = product.Sku,
                    ImageLink   = DiskStorage.ProductImageUrlMedium(HccApp, product.Bvin, product.ImageFileMedium, Request.IsSecureConnection),
                    Link        = productUrl,
                    MobileLink  = productUrl,
                    Price       = string.Format(Constants.CURRENCY_TEXT_FORMAT, product.SitePrice.ToString(Constants.CURRENCY_FORMAT), regionInfo.ISOCurrencySymbol),
                    ProductType = GetFirstAvailableCategory(HccApp, product.Bvin), // put your preferred site category here
                    Title       = product.ProductName,
                    MPN         = product.Sku,                                     // this should be replaced with real data
                    Brand       = product.VendorId,                                // this should be replaced with real data
                    Color       = string.Empty,
                    Gender      = Constants.GENDER_UNISEX,                         // OPTIONS: male, female, unisex
                    AgeGroup    = Constants.AGE_GROUP_ADULT,                       // OPTIONS: newborn, infant, toddler, kids, adult
                    GTIN        = GenerateGTIN()                                   // this should be replaced with real data
                };

                // this could and should be iterated on to show shipping options for up to 100 locations
                productEntry.Shipping = new List <ShippingInfo>();
                productEntry.Shipping.Add(shippingInfo);

                // this could and should be iterated on to show taxes for up to 100 locations
                productEntry.Tax = new List <TaxInfo>();
                productEntry.Tax.Add(taxInfo);

                feed.Entries.Add(productEntry);
            }

            // simply done to display the feed in the textbox
            // alternatives could be to send this through a web service or other means
            txtGoogleFeed.Text = feed.SerializeObject();
        }