Thursday, March 11, 2010

How to access the Targeting Context from within a Commerce Server Basket Pipeline

I fixed a bug yesterday where an order-level discount targeted against Targeting Context.Subtotal had an incorrect value—it was set in code before running the basket pipeline, but the subtotal was inaccurate before running the pipeline (newly added items to the basket had a price of zero prior to the pipeline execution, for example).

The solution was to update the subtotal in the targeting context during pipeline execution, after fetching the latest pricing data from the catalog and before running the OrderDiscount pipeline component. Here’s the code to get at the targeting context from within a pipeline:

public int Execute(object pdispOrder, object pdispContext, int lFlags)
    IDictionary context = (IDictionary)pdispContext;
    Decimal subtotal = (decimal)((Microsoft.CommerceServer.Interop.Profiles.IProfileObject)context["ContextProfile"]).Fields["Subtotal"].Value;

No comments: