Filtering category products that are out of stock

A question was asked how to configure Commerce Server 2009's default site to filter out products where the OnHandQuantity=0.

This filter setting is stored in the CategoryConfiguration object which Commerce Server passes into the core subsystems when retrieving a category.

Although you cant change the out of the box component, you can change the CategoryConfiguration object that it uses.

When Commerce Server retrieves a category it first checks the operationCache for a copy of the CategoryConfiguration and then creates a default one, if it is not found.

If you want to override the default behavior, what you need to do is create an OperationSequenceComponent that executes right before the “Category_Loader” OSC in the QueryOperation_Category operation like this:

This is your ChannelConfiguration.config:

      <MessageHandler name="CommerceQueryOperation_Category" responseType="Microsoft.Commerce.Contracts.Messages.CommerceQueryOperationResponse, Microsoft.Commerce.Contracts, Version=, Culture=neutral,PublicKeyToken=31bf3856ad364e35">


      <!—This one is added à

          <Component name="OnHandFilter" type="Microsoft.Commerce.Samples.OnHandFilter, OnHandFilter, Version=, Culture=neutral,PublicKeyToken=62d299e5f36470e7">

            <!—This one is added à

          <Component name="Category_Loader" type="Microsoft.Commerce.Providers.Components.CategoryLoader, Microsoft.Commerce.Providers, Version=, Culture=neutral,PublicKeyToken=31bf3856ad364e35" />



The component would look like this:

public class OnHandFilter:OperationSequenceComponent


        public override void ExecuteQuery(Microsoft.Commerce.Contracts.Messages.CommerceQueryOperation queryOperation, Microsoft.Commerce.Broker.OperationCacheDictionary operationCache, Microsoft.Commerce.Contracts.Messages.CommerceQueryOperationResponse response)


            var categoryConfiguration = new Microsoft.CommerceServer.Catalog.CategoryConfiguration();

            categoryConfiguration.InventoryOptions = new Microsoft.CommerceServer.Inventory.InventoryOptions();

            categoryConfiguration.InventoryOptions.FilterOutOfStockSkus = true;

            operationCache.Add("CategoryContentSelectorKey_D6E2DE05-01E0-4a35-BF26-BCF95971B8D1", categoryConfiguration);



You can actually override any of the CategoryConfiguration properties this way.

I tested the above on our out of the box site and it does what you are looking for.

You could further enhance this by making a single generic OperationSequenceComponent that exposes those configuration properties in the CategoryConfiguration object as configurable properties.

I will explore that in my next blog entry.