Hi Mike,
There is a package already developed for this need, see https://github.com/Geta/GoogleProductFeed which I believe is for the version of CMS and Commerce you are using. If you are using a newer version see here also https://github.com/Geta/geta-optimizely-productfeed.
If you find you have thousands of products and it takes a long time to run the job, it could be worth looking at using search and navigation to retrieve products if your using it. Make sure to use projections and that should make the search queries more efficient.
Hope it helps
Paul
This could come in handy Getting all non published variations - Quan Mai's blog (vimvq1987.com)
Note that creating a big list of products can have serious performance consequences
You can use what @Quan suggested. Or else if you have a large list of products, nodes and variants, I would recommend using DTOs or CatalogContext, which is much faster compared to ContentRepository or ContentLoader. E.g. These are some of the APIs available.
For context: what I'm trying to do is have a job that runs and builds an XML file for a Google Shopping Feed based on all the published products on our site.
I have a job that loops through a JSON file list of products, looks up the product in the db based on the PartNumber, and then updates the product in the db with the fields from the JSON file.
The first thing I'm looking to do, but am unfamiliar with, is return all products on the site to a list in code. I want any product that is purchasable on the site to be return to a list, and from there I can figure the rest out. Can't seem to find any code to work for this in the mean time.
This code isn't my attempt at it, just how I was getting the products by individual lookup - am looking for a way similar to this but in bulk. Ultimately, getting our purchaseable products into an XML for the purposes of a Google Shopping Feed file is the ultimate goal, so if there is a better solution for that please let me know! Thank you!