IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     
 
developerWorks
AIX and UNIX
Information Mgmt
Lotus
New to Lotus
Products
How to buy
Downloads
Live demos
Technical library
Training
Support
Forums & community
Events
Rational
Tivoli
WebSphere
Java™ technology
Linux
Open source
SOA and Web services
Web development
XML
My developerWorks
About dW
Submit content
Feedback



developerWorks  >  Lotus  >  Technical Library
developerWorks

[back to Anatomy of a Domino e-commerce Web site (Part 2)]

The AddtoCart agent
Here is the code for the AddtoCart agent:

Sub Initialize
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument, oiDoc As NotesDocument, cDoc As NotesDocument
    Set db = s.CurrentDatabase

    Set doc = s.DocumentContext

    Dim vCartID As Variant, vISBN As Variant, vItemID As Variant, vMedia As Variant, vQuantity As Variant, vPosn As Variant, vPath As Variant
    Dim vOrderKey(1) As String

    'Determine CartID
    vCartID = Evaluate ({ @Middle (@LowerCase(Query_String) + "&"; "&cartid="; "&") }, doc)
    If vCartID(0) = "" Then
      vCartID= Evaluate({@Middle(@LowerCase(HTTP_COOKIE) + ";";"cartid=";";")}, doc)
    End If
    'Get ISBN from URL
    vISBN = Evaluate ({ @Middle (@LowerCase(Query_String) + "&"; "&isbn="; "&") }, doc)

    'Get the catalog document using the ISBN as the key
    Set cDoc = db.GetView ("ISBNLookup").GetDocumentByKey (vISBN(0))

    ' Get the existing order item document, if it exists.
    vOrderKey(0)=vCartID(0)
    vOrderKey(1)=vISBN(0)
    Set oiDoc = db.GetView ("OrderISBNLookup").GetDocumentByKey (vOrderKey)

    ' Create new order item document in the event one was not found
    If oiDoc Is Nothing Then
      Set oiDoc = db.CreateDocument
      oiDoc.Form = "OrderItem"
      oiDoc.CartID = vCartID
      oiDoc.Quantity = 1
      oiDoc.ISBN = vISBN
      oiDoc.Title = cDoc.Title
      oiDoc.Author = cDoc.Author

      'Get position of media type from catalog and retrieve
      cDoc.tempISBN = vISBN
      vPosn = Evaluate ({@Member (tempISBN; MediaISBNs)}, cDoc) ' position of ISBN in catalog entry doc
      cDoc.tempPosn = vPosn
      vMedia = Evaluate ( {@If (tempPosn = 0; "Error"; @Subset (@Subset (MediaTypes; tempPosn); -1))}, cDoc)
      oiDoc.Media = vMedia
    Else
      oiDoc.Quantity=oiDoc.Quantity(0) + 1
    End If

    'Even if this is an existing Order Item we can go ahead and update the information. Possibly prices have
    'changed.
    cDoc.tempISBN = vISBN
    vPosn = Evaluate ({@Member (tempISBN; @lowercase (MediaISBNs))}, cDoc) ' position of ISBN in catalog entry doc
    cDoc.tempPosn = vPosn
    vPrice = Evaluate ( {@If (tempPosn = 0; "Error"; @Subset (@Subset (MediaPrices; tempPosn); -1))}, cDoc)
    oiDoc.Price = vPrice

    Call oiDoc.Save (True, True)

    'Set URL path for return
    vPath=Evaluate({@ReplaceSubstring (@Subset (@DbName; -1); "\\" : " "; "/" : "+")})
    Print "[" + vPath(0) + "/cart?ReadForm&CartID=" + vCartID(0) + "]"

End Sub
    About IBM Privacy Contact