In my last post, I showed how to create an OperationSequenceComponent (OSC) that allows you to alter the default behavior when a category is retrieved so that out of stock products are filtered out.
Unfortunately, I had to hard code the setting into the OSC like this:
categoryConfiguration.InventoryOptions.FilterOutOfStockSkus = true;
Now, if I ever want to change that setting, I have to go in and change the code, recompile and redeploy. It would be great if I could make that configurable so that I can change it through configuration whenever I want without having to alter the code.
Fortunately, Commerce Server Operation Sequence Components have a way to facilitate configuration quite easily.
To start with, we take the OSC that I created in my last post:
var categoryConfiguration = new Microsoft.CommerceServer.Catalog.CategoryConfiguration();
Next, we need to create a class to store the configurable elements:
Next, we need to implement the IConfigurable interface into the OnHandFilter class:
protected Configuration ComponentConfiguration;
public void Configure(System.Configuration.ConfigurationElement configuration)
Now, we change our hard coded value to read from the configuration:
categoryConfiguration.InventoryOptions.FilterOutOfStockSkus = ComponentConfiguration.FilterOutOfStockSkus;
You can now compile and deploy this component to the GAC.
Next, open up your ChannelConfiguration.config file and go find the “QueryOperation_Category” section that was modified in the previous blog post to add the new OnHandFilter OSC. Alter the entry to include the new configuration setting.
<Component name="OnHandFilter" type="Microsoft.Commerce.Samples.OnHandFilter, OnHandFilter, Version=18.104.22.168, Culture=neutral,PublicKeyToken=62d299e5f36470e7">
So we are ready to test this now. To test it, we take the Adventureworks, out of the box contemporary site and we modify the Polar Star sleeping bag to have an OnHandQuantity of 0.
Now when go to the Sleeping Bags category, we should no longer see the Polar Star product returned:
Now, if you go back and set the FilterOutOfStockSkus to False, the product should show back up again:
Now you can extend this concept to add configuration points for all of the settings available in the CategoryConfiguration object.