Return all products to a list

Vote:
 

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!

private void ProcessInventoryUpdate(ProductInventory inventoryUpdate, InventoryStats stats)
        {
            var productLink = ProductHelper.GetCatalogEntryByMetaField("PartNumber", inventoryUpdate?.ItemId);

            if (productLink == null)
            {
                stats.ProductsNotFound++;
                return;
            }

            // Get the product as CatalongBase from the content loader so I can modify the SiteProductVariation settings
            var product = _contentLoader?.Get<CatalogContentBase>(productLink?.ContentLink) as SiteProductVariation;

            if (product?.CreateWritableClone() is SiteProductVariation writableProduct)
            {
                UpdateProduct(writableProduct, inventoryUpdate, productLink.Code, stats);
            }

            UpdateInventoryRecord(productLink.Code, inventoryUpdate, stats);

            stats.TotalProcessed++;
        }
#331294
Oct 11, 2024 14:51
Vote:
 

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

#331348
Oct 12, 2024 21:10
Vote:
 
#331424
Oct 14, 2024 9:24
Vote:
 

Note that creating a big list of products can have serious performance consequences 

#331428
Oct 14, 2024 14:52
Vote:
 

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. 

  • CatalogContext.Current.GetCatalogNodesDto
  • CatalogContext.Current.GetCatalogEntriesDto
  • CatalogContext.Current.GetCatalogEntryDto
#331747
Edited, Oct 20, 2024 23:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.