Quantcast
Channel: Customized Commerce 13 and earlier versions
Viewing all articles
Browse latest Browse all 9642

Getting a product based on a MetaFieldName of "PartNumber"

$
0
0

I have a JSON file being read and loaded into a List, which then I need to loop through and query for a product where the "PartNumber" == inventoryUpdate.ItemId. Using some old code in a past devs helper class works to match the PartNumber to the product, but doesn't contain the "AvailableInventory" property I need to set in this loop. The end goal is to loop thru the JSON file, match ItemId to PartNumber meta field, and update the "AvailableInventory" for that product.

Containing the for loop

protected override string Execute(SiteDefinition siteDefinition)
        {
            try
            {
                var inventoryUpdates = productInventoryRepository.GetAll();
                var numUpdated = 0;
                // Prevent multiple enumerations of the same inventory
                var productInventory = inventoryUpdates.ToList();
                foreach (var inventoryUpdate in productInventory)
                {
                    if (_stopSignaled) break;
                    // match the ItemId to the PartNumber property for the product
                    var product = ProductHelper.GetCatalogEntryByMetaField("PartNumber", inventoryUpdate.ItemId);
                    // Can't access the AvailableInventory meta field to set it with the "product" object above.
                }
                return $"{numUpdated} of {productInventory.Count()} pricing updates applied.";
            }
            catch (FormatException)
            {
                return "Pricing update failed due to incorrect format of data.";
            }
        }

Existing product helper class methods

public class ProductHelper : HelperBase
    {
        #region Properties
        #endregion Properties
        public static CatalogEntry GetCatalogEntryByMetaField(string metaFieldName, string metaFieldValue)
        {
            CatalogSearchOptions searchOptions = new CatalogSearchOptions();
            searchOptions.RecordsToRetrieve = 10;
            CatalogSearchParameters searchParameters = new CatalogSearchParameters();
            searchParameters.JoinType = "INNER JOIN";
            searchParameters.JoinSourceTable = "CatalogEntry";
            searchParameters.JoinTargetQuery = "(SELECT DISTINCT ObjectId, PartNumber FROM CatalogEntryEx_ProductVariation) ProductVariation";
            searchParameters.JoinSourceTableKey = "CatalogEntryId";
            searchParameters.JoinTargetTableKey = "ProductVariation.ObjectId";
            searchParameters.SqlWhereClause = string.Format("CatalogEntry.IsActive = 1 AND ProductVariation.{0} = '{1}'", metaFieldName, metaFieldValue);
            int count = 0;
            CatalogEntryDto entries = CatalogContext.Current.FindItemsDto(searchParameters, searchOptions, ref count, new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull));
            if (count == 0 || entries.CatalogEntry == null || entries.CatalogEntry.Count == 0)
                return null;
            return CatalogEntry.Create(entries.CatalogEntry[0].CatalogEntryId);
        }

Viewing all articles
Browse latest Browse all 9642

Trending Articles