Credits: How to make a Google Shopping Feed with C# and serve it through the Web API.
This will create a Google Product Feed based on the Atom specification. For information on what is required and what the different attributes/properties mean, please see the Product data specification.
Install-Package Geta.GoogleProductFeed
Note that you need to make sure your projects calls config.MapHttpAttributeRoutes(); in order for the feed routing to work.
Default URL is: /googleproductfeed
You need to implement the abstract class FeedBuilder and the method Build. This will provide the feed data.
public class EpiFeedBuilder : FeedBuilder
{
public override Feed Build()
{
var feed = new Feed
{
Updated = DateTime.UtcNow,
Title = "My products",
Link = "https://mysite.com"
};
var catalogReferences = _contentLoader.GetDescendents(_referenceConverter.GetRootLink());
var entries = new List<Entry>();
var market = _currentMarket.GetCurrentMarket();
var currency = _currencyservice.GetCurrentCurrency();
foreach (var catalogReference in catalogReferences)
{
var catalogContent = _contentLoader.Get<CatalogContentBase>(catalogReference);
// ReSharper disable once IsExpressionAlwaysFalse
if (catalogContent is DefaultVariationContent)
{
var variationContent = (DefaultVariationContent)catalogContent;
var defaultPrice = ProductHelper.GetDefaultPrice(variationContent, market, currency);
var entry = new Entry
{
Id = variationContent.Code,
Title = variationContent.DisplayName,
Description = variationContent.Description.ToHtmlString(),
Link = variationContent.ContentLink.GetFriendlyUrl(true),
Condition = "new",
Availablity = "in stock",
Brand = variationContent.Brand,
MPN = "",
GTIN = variationContent.GTIN,
GoogleProductCategory = "",
Shipping = new List<Shipping>
{
new Shipping
{
Price = "Free",
Country = "US",
Service = "Standard"
}
}
};
string image = variationContent.GetDefaultAsset<IContentImage>();
if (!string.IsNullOrEmpty(image))
{
var imageUrl = new Url(image);
entry.ImageLink = imageUrl.ToAbsoluteUri().ToString();
}
if (defaultPrice != null)
{
var discountPrice = ProductHelper.GetDiscountPrice(defaultPrice, market, currency);
entry.Price = defaultPrice.UnitPrice.ToString();
entry.SalePrice = discountPrice.ToString();
entry.SalePriceEffectiveDate = DateTime.UtcNow;
}
entries.Add(entry);
}
}
feed.Entries = entries;
return feed;
}
}
Then you need to use this as the default implementation for FeedBuilder. Using StructureMap it will look something like this in your registry class:
For<FeedBuilder>().Use<EpiFeedBuilder>();
Make sure dependency injection is setup for Web API. The quickest way to do this is install the package: Foundation.WebApi.
Populating the feed is handled through a scheduled job and the result is serialized and stored in the DDS. See job 'Google ProductFeed - Create feed' in admin mode.
If your request to /googleproductfeed returns 404 with message 'No feed generated', make sure you run the job to populate the feed.