Friday, June 11, 2010

Using Commerce.Scriptor pipeline component in Microsoft Commerce Server 2007/09

Commerce.Scriptor pipeline component is used to inject small functionality into the pipeline. This component is generally used to verify the values and perform some arithematic operations on the values in orderform object. By using scriptor component we can perform small operations like applying certain tax % for all users, change price of the product based on some key in product info.

This component is not used where we need to access information from other sources like getting data from database or accessing other modules in commerce server using API.

We can pass key/Value pairs to the component using its interface. We can access these key/value pairs from 'config' parameter of MSCSExecute method.

Here are the few links from mdsn to configure and use scriptor component

http://msdn.microsoft.com/en-us/library/ms946865(CS.70).aspx
http://msdn.microsoft.com/en-us/library/ms944284(CS.70).aspx

Examples for using Scriptor component -

Applying value in 'DiscountPrice' property as product price if it is not null:

Requirement:

For every product there is a property called 'DiscountPrice'. If that property is not null then the value in that property should be considered as product price.

What to do:

Create a pipe line component which replaces the values for dictionary keys _cy_iadjust_regularprice, cy_placed_price if that is not null. To know more about the dictionary keys in order form got to http://msdn.microsoft.com/en-us/library/ms962766(CS.70).aspx.

Where to add the pipeline component:

In basket pipeline 'Query catalog info' queries the for catalog info from catalog module and creates the dictionary keys for all product properties like '_product_*' (* being name of the property). In 'Item price' stage after the 'requiredItemPriceCy' we have to add the scriptor component because in the 'requiredItemPriceCy' component values for the above said dictionary keys are calculated. we have to replace the calculated values.

(To know more about the commerce server pipeline components in all pipelines goto http://msdn.microsoft.com/en-us/library/ms916292(CS.70).aspx ).

How to add the pipeline component:
  1. Open basket pipeline of the application. 
  2. Go to Item price stage and right-click on requiredItemPriceCy component and insert component -> after. 
  3. Select stages as current stage and select commerce scriptor component and click ok. 
  4. Go to component properties and scriptor tab. 
  5. Check the internal radio and click on edit. 
  6. It will open editor window where we need to copy the code in VB script. 
  7. Copy the below code in the editor and click ok. 
  8. Restart IIS. 
Coding in scriptor component:

There are three functions to implement using scriptor component
  1. MSCSExecute 
  2. MSCSOpen 
  3. MSCSClose
We have good documentation on these on above said MSDN links.

The only method that is compulsory and required is MSCSExecute. This method is used to implement the desired functionality. This method have four parameters namely
  1. config - Contains key/value pairs entered on the pipeline interface. 
  2. orderform - Contains key/value pairs of orderform (or basket). 
  3. context - Contains key/value pairs of context 
  4. flags
We have good documentation on these on http://msdn.microsoft.com/en-us/library/ms944284(CS.70).aspx.

How does Code Works:

orderform parameter is of type dictionary. Iterate through each Item and look for the key( key need not be present for every item as it depends on product definition). Read the value and if it is not null then change the values of the price keys to this value.
function MSCSExecute(config, orderform, context, flags)
   Dim x
   for each item in orderform.Value("Items")
     for each key in item
     if key="_product_DiscountPrice" then
      x = item.Value("_product_DiscountPrice")
      if IsNull(x) then
      else
         if IsNumeric(x) then
           item.Value("_cy_iadjust_regularprice") = x
           item.Value("cy_placed_price") = x
        end if
      end if
      Exit for
     end if
   next
  next
 MSCSExecute = 1
end function

sub MSCSOpen(config)
end sub

sub MSCSClose()
end sub

No comments:

Post a Comment