How to Create a Shipping Cost Calculator
A shipping cost calculator is a pipeline component that calculates the cost to ship items via particular shipping method. If you incorporate a custom way of charging for shipping into your Commerce Server Core Systems application, you must create a new shipping cost calculator.
Create the pipeline component by following the instructions in the topic How to Build Pipeline Components Using C#. The following procedure describes how to implement the aspects of a pipeline component that are required for a shipping cost calculator. You can find a sample shipping cost calculator in the %COMMERCE_SERVER_ROOT%\Sdk\Samples\MinMaxShipping folder.
To create a shipping cost calculator
Define a class that implements the IPipelineComponent interface. If the pipeline component will store configuration information, also implement the IPersistDictionary interface. Configuration information is information about the pipeline component itself instead of information that is only needed during a single execution of the pipeline component.
Define a constructor for the class.
Define a method named GetProgID that returns the pipeline component's programmatic identifier (ProgID). The ProgID can be any unique string, although it is usually related to the namespace and class name.
Define the IPipelineComponent::Execute method. Within the Execute method, do the following:
Cast the arguments to the method to the IDictionary interface.
(IDictionary) context = (IDictionary) contextArgument;
(IDictionary) orderForm = (IDictionary) orderFormArgument;
Where contextArgument and orderFormArgument are the arguments that were passed to the Execute method.
If the pipeline component might generate errors that will be displayed to the customer, get the MessageManager object.
messageManager = (IMessageManager)context["MessageManager"];
Get the data that you will need in order to calculate the cost from the order form and the context. In particular, you can get the shipments by using the following code:
(ISimpleList) shipmentsList = (ISimpleList)context["shipments_to_process"];
Iterate through the shipments, and execute whatever business logic is necessary to calculate the cost of each shipment. You can iterate through the shipments by using the following code:
foreach (IDictionary shipment in shipmentsList)
Where shipmentsList is the list of shipments that you obtained in step c.
Set the value of shipment["_cy_shipping_total"] to the cost of the shipment.
If there are errors that you want to display to the customer, use the MessageManager object that you created in step b to create an error message and add it to the errors that are associated with the basket.
Object errorMessage = messageManager.GetMessage(errorName, null);
((ISimpleList)orderForm["_Basket_Errors"]).Add(ref errorMessage);
Where errorName is a string that contains the identifier of the error, and orderFormArgument is the order argument that you cast to the IDictionary interface in step a.
If the shipping cost calculator might generate any new errors to display to the customer, add the error messages to MessageManager. For more information about how to customize error messages, see How to Use MessageManager.
See Also
Other Resources
How to Build Pipeline Components Using C#