|
Chapter 15: Order Processing
Chapter 15 Order ProcessingAs your shopper moves through the purchase process, there are a number of common tasks that you'll need to perform. You'll use the OrderGroup, OrderForm, Dictionary, SimpleList, and other objects to aid in processing the order. In addition, you'll probably have to write some SQL stored procedures and perform queries using ActiveX Data Object (ADO). You'll certainly make use of pipelines, which are covered in the next few chapters, to assist in processing the order through the system.
Creating the OrderGroup ObjectAs Chapter 14, "Order Basics," pointed out, the OrderGroup object is the central element to working with orders in your system. The first step in working with the object is to create and initialize it. You'll create the object using the PROGID Commerce.OrderGroup and initialize it by providing the database connection string to the database that holds the order information (see the following code sample). You also provide a user ID that you are using to open the order. Typically, this is the user ID of the currently logged in user. However, if this user is an administrator, he or she can open another user's order and must specify that user's ID.
Set mscsOrderGroup = Server.CreateObject("Commerce.OrderGroup")
mscsOrderGroup.Initialize _ mscsOptionsDictionary.s_TransactionConfigConnectionString, _ mscsUserProfile("generalinfo.user_id").value
Working with the BasketThere are a number of tasks that you'll need to complete when working with the user's basket or shopping cart, which are detailed in the following sections.
Creating a New BasketYou can create a new basket by loading and initializing an OrderGroup. If there are no OrderForms in the OrderGroup, you can create a default OrderForm. To do so, you'll create an OrderForm object and then add it to the OrderGroup using the AddOrderForm method, shown in the following code sample:
if mscsOrderGroup.Value.orderforms.count = 0 then
set mscsOrderForm = server.CreateObject("Commerce.OrderForm")
call mscsOrderGroup.AddOrderform(mscsOrderForm,"default")
mscsOrderGroup.SaveAsBasket
mscsOrderGroup.LoadBasket
end if
Retrieving a BasketYou can retrieve the shopper's current basket by using the OrderGroup's LoadBasket method, shown here:
Call mscsOrderGroup.LoadBasket()
The basket is identified by the user's ID in the database, and that ID is used
Retrieving the OrderForm from the OrderGroupAt times, you might need to retrieve an individual OrderForm from the OrderGroup. The OrderForms within the OrderGroup are identified by name of the trading partner with which they're associated. If the OrderForm is not associated with a trading partner, the name will be default, as shown here:
Set mscsOrderForm = mscsOrderGroup("OrderForms").Value("default")Another syntax that performs the same function is the following:
Set mscsOrderForm = mscsOrderGroup.Value.OrderForms("default")
This is because the OrderGroup is another special implementation of the You can also identify the number of OrderForms that are in the OrderGroup using this code:
lCount = mscsOrderGroup.Value.OrderForms.Count
Displaying the BasketBuilding a basket page entails retrieving the data from each OrderForm and listing them on the page. Of course, you must recognize that many of the values you might want to display are added to the OrderForm through various pipeline components. In particular, item data is completed by being retrieved from the database or being calculated. For instance, the line item subtotal (_cy_oadjust_adjustedprice) is calculated based on the quantity of items (quantity) being ordered and the current price (_cy_iadjust_currentprice) of the item.
Displaying a Single OrderForm Basket To start, you can directly build a page that displays the order entry by retrieving the desired values and retrieving each of the items from the order. You'll start by retrieving the Basket from the OrderGroup object, as shown in the following code sample:
set mscsOrderGroup = Server.CreateObject("Commerce.OrderGroup")
mscsOrderGroup.Initialize Application("sTransactionCS"), _
mscsOrderGroup.LoadBasket You'll then run the basket pipeline, which is covered in more detail in the following chapters. For now, you'll have to accept this at face value. The pipeline helps populate some of the calculated keys in the OrderForm, using the code shown here:
Call RunPipe("basket", mscsOrderGroup)Then, you'll retrieve the default OrderForm and parse through it to get the item values that you want to display, as shown in in the following code sample:
Set mscsOrderForm = mscsOrderGroup.Value.OrderForms("default")htmItems = "<table><tr>" _
For i = 1 to mscsOrderForm.Items.Count
Set item = mscsOrderGroup.GetItemInfo(i-1, "default")
htmItems = htmItems & "<tr>" _ next
Finally, you can retrieve the order-level data, which in the case shown in the following code, is the subtotal for the order. You can then display the entire basket contents.
htmItems = htmItems & "<tr>" _
htmItems = htmItems & "<tr>" _
htmItems = htmItems & "</table>"
htmPage = "<HTML><HEAD></HEAD><BODY>" _
Response.Write htmPage Displaying a Multiple OrderForm Basket Of course, if you're attempting to display items that are listed in separate OrderForms within the OrderGroup, you'll have to parse through each OrderForm to retrieve the items from each. You'll also need to retrieve the OrderForm values, such as the subtotal and other values, for each OrderForm and aggregate those as well. To accomplish this, you can modify the previous code to loop through each OrderForm in the OrderGroup and gather the selected data, as shown in the following code:
set mscsOrderGroup = Server.CreateObject("Commerce.OrderGroup")
mscsOrderGroup.Initialize Application("sTransactionCS"), _
mscsOrderGroup.LoadBasket
Call RunPipe("basket", mscsOrderGroup)
htmItems = "<table><tr>" _
cySubTotal = 0.0
For each OrderFormName in mscsOrderGroup.Value.OrderForms
Set mscsOrderForm = _
For i = 1 to mscsOrderForm.Items.Count
Set item = mscsOrderGroup.GetItemInfo(i-1, "default")
htmItems = htmItems & "<tr>" _
next
cySubTotal = cySubTotal + mscsOrderForm("_cy_oadjust_subtotal")
Next
htmItems = htmItems & "<tr>" _
htmItems = htmItems & "<tr>" _
htmItems = htmItems & "</table>"
htmPage = "<HTML><HEAD></HEAD><BODY>" _
Response.Write htmPage
Running a PipelineTo populate the nonpersistent and calculated values in the order, you'll run an appropriate Order Processing Pipeline by calling the RunPipe method of the OrderGroup object, as shown here:
Call mscsOrderGroup.RunPipe(sPath & sPipeFile, _
Adding an Item to the BasketTo add an item to the user's basket, you'll first build a Dictionary object that holds all of the desired item data. At a minimum, you must provide the following fields:
You'll then call the OrderGroup's AddItem method, providing the Item Dictionary and the name of the OrderForm to which the item should be added, as seen here:
set dItem = Server.CreateObject("Commerce.Dictionary")
dItem.product_id = _
dItem.quantity = iQty
dItem.product_catalog = sCatalogName
dItem.product_catalog_base = sCatalogName
mscsOrderGroup.AddItem dItem, "default"
mscsOrderGroup.SaveAsBasket
Removing an Item from the BasketIf a user wants to remove an item from his or her basket, you'll need to provide a means to do so. To do this, you specify the index value for the line item in the RemoveItem method of the OrderGroup, as shown in the following code:
set mscsOrderGroup = Server.CreateObject("Commerce.OrderGroup")
mscsOrderGroup.Initialize Application("sTransactionCS"), _ m_UserGUID
mscsOrderGroup.LoadBasket
iLineItem = Request.QueryString("i")
Call mscsOrderGroup.RemoveItem(i, "default")
mscsOrderGroup.SaveAsBasket
Removing All Items from the BasketA user might want to empty his or her basket completely, removing all of the items. This can be done by calling the RemoveItem method without specifying a line item index, as shown in in the following code:
set mscsOrderGroup = Server.CreateObject("Commerce.OrderGroup")
mscsOrderGroup.Initialize Application("sTransactionCS"), _
mscsOrderGroup.LoadBasket
Call mscsOrderGroup.RemoveItem(,"default")
mscsOrderGroup.SaveAsBasket
Aggregating Items in the BasketA shopper might add an item to the basket more than once. You can leave the item in the basket multiple times, but you might want to combine those line items so that the item only appears once in the basket. Of course, you'll need to modify the quantity for the item to reflect each of the instances of the item in the OrderForm.The first thing you'll do is retrieve the Basket and OrderForm for which you'll be aggregating values. You'll also need a Dictionary object that will hold each of the items being processed, as shown in in the following code. Each item will be held in the Dictionary with a unique key, based on the product_id.
set mscsOrderGroup = Server.CreateObject("Commerce.OrderGroup")
mscsOrderGroup.Initialize Application("sTransactionCS"), _
mscsOrderGroup.LoadBasket
Set mscsOrderForm = mscsOrderGroup.Value.OrderForms("default")
set dItem = createobject("Commerce.Dictionary")
set slItems = createobject("Commerce.SimpleList") You'll then loop through each item in the OrderForm. If the item has not yet been added to the temporary Item Dictionary, you'll add it, as shown here:
i = 0
For Each Item In mscsOrderForm.items
If IsNull(dItem(Item.product_id)) Then
'this is a new product, just add the dictionary to it
i = i + 1
Item.lineitem_id = i
Set dItem(Item.product_id) = Item If the item has already been added, you'll increment the quantity in the item in the temporary Item Dictionary by the amount in the current instance of the product, as shown in the following code:
Else
'this product exists, add the quantities
Set d = dItem(Item.product_id)
d.quantity = cint(d.quantity) + cint(Item.quantity)
Set dItem(Item.product_id) = d
End If
Next After you've parsed through each of the items in the Basket, you'll build a SimpleList of each of the aggregated items, as shown in in the following code. This list is then saved back into the OrderForm, replacing the existing Items SimpleList.
For Each vItem In dItem
slItems.Add dItem(vItem)
Next
Set mscsOrderForm.items = slItems
mscsOrderGroup.SaveAsBasket
Saving the BasketWhen you make a change to the basket, you must remember to call the SaveAsBasket method of the OrderGroup, shown here, to save those changes to the database:
mscsOrderGroup.SaveAsBasket Otherwise, when you leave the current page and the OrderGroup object goes out of scope, the changes are lost.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||