Submitting the cart contents to a database

At some point, the customer is given the option to submit the order. At this time, the contents of the cart can be inserted into a data source. Alternatively, for small eBusinesses the contents of the cart could be e-mailed to the store owner as an order, using a mail component.

For larger applications, order information is usually stored in a database. The structure of the database will depend on the requirements of the application. However, to use the shopping cart, the database table must have:

A unique ID field to identify the order (usually OrderID)
Although not absolutely required, usually you also want to include:
A Product ID field that corresponds to the Product ID stored in the shopping cart
A Quantity field that corresponds to the Quantity field stored in the shopping cart

A Price field may also be included to correspond to the Price field stored in the shopping cart, although this field might conceivably be omitted if the price is stored elsewhere in the database and is a constant that would not change. Optionally, you can include the Total and any other columns from the shopping cart that are required to fill the order (size, color, and so on).

This table (an OrderDetails table) is only storing the contents of the cart, one record for each item in the cart. A typical shopping cart will have several items in it. The Unique ID field, or OrderID uniquely identifies the contents of this cart in this table. A SQL statement of the form SELECT * FROM OrderDetails WHERE OrderID=17 will return the contents of the shopping cart for OrderID 17, or invoice number 17. There will probably be more than one record in this result set.

Of course, you will want to collect other data, too, including customer, shipping, and payment information. However, to keep the cart as versatile as possible, those data components are not included nor required in the MX shopping cart. They can easily be plugged in from other data sources as needed. Some users may use a login to identify registered customers with established accounts only, others may collect all information for each order online, and still others may use a combination of stored and newly gathered information.

A common database schema includes an Orders table which is related one-to-many to an OrderDetails table. The OrderDetails table contains the contents of a cart as described above. The Orders table contains one record for each order and contains the customer information, shipping information, and most important an OrderID, or invoice number. The OrderID is unique in the Orders table. Once you identify an order (a record) in the Orders table (perhaps by customer ID, or date) then you can enumerate the contents of that order via the SELECT statement above.

The server behavior Save cart to table can be used to interact with an orders database. However, before applying this behavior, you should consider whether this is the most appropriate method for your application. To use this behavior into your database you will have to decide on the method you will use to generate an OrderID, according to your database structure and application. Also, bear in mind that if you are using a third-party solution for credit card handling, you may want to use a different behavior compatible with that data-handling method.


Using the Save Cart to Table behavior

The Save Cart to Table behavior can be applied to any page that contains the shopping cart to submit the contents of the cart to your database.

The following steps are required to use this behavior:

Create a table in your database that contains a single numerical field and a single record that can be used for autonumbering.
Apply the behavior Get Unique ID From Table to retrieve the value to use for the OrderID and put it in a session object.
Put the OrderID session variable in a hidden form element. (This step is necessary in order to use the Insert Record behavior.)
Apply the standard Insert Record behavior.
Apply the Save Cart to Table behavior.

Creating an Order ID
In your database, create a table with a single numerical field. Create one record and enter a starting number as a numerical key. This key will be auto-incremented each time it is accessed through the Get Unique ID from Table behavior.

Now you can apply the server behavior to get the unique ID and store it in a session variable to use when the order is inserted into the database.

To get the Unique ID and store it in a session variable:

1 In the Data Bindings palette, click the plus (+) button and select Session Variable. In the dialog box, name the session variable (for example, OrderID) and click OK.
2 In the Server Behavior inspector, click the plus (+) button and choose MX Shopping Cart > Get Unique ID from Table.
3 In the dialog box, select the Connection. Then select the table and the column that contains your numerical key.
4 Select the session variable in which to store the value by clicking the lightning bold next to the Session var box and selecting the OrderID session you created. Then click OK.

5 Drop a hidden form element on the page from the Object palette, Forms tab. Give it an appropriate name, such as HiddenOrderID.
6 Open the Data Bindings palette. Expand the Session object and select the OrderID session variable.
7 With the hidden form element selected, select the OrderID session object in the Data Bindings palette and click Bind.

Inserting the order into the database and submitting the order
To insert the order into the database:

1 In the Server Behavior inspector, click the plus (+) button and choose Insert Record.
2 In the dialog box, choose the connection. Then choose the table into which to insert the order record.
3 Choose the form. Then choose the OrderID column in the database into which the unique ID stored in the variable will be inserted.

Note: Do not enter a Go To URL to redirect users here. If you want to redirect users after the order is submitted, you can choose the URL when you apply the Save Cart to Table behavior next.

To submit the cart contents to the database:

1 Place a form button on the page to submit the data, if you do not already have one.
2 Select the form button. In the Server Behavior inspector, click the plus (+) button and choose MX Shopping Cart > Save Cart to Table.
A dialog box opens in which you can bind the columns in the shopping cart to the columns in the database.

3 Select the data connection. Then select the table in the data source into which the cart contents are to be inserted.
4 Select a cart column, then select the Destination column in the database. Do this for each column in the cart that is to be inserted into the database.
You do not need to map every column in the shopping cart—only those that you want to insert into the database table.
5 Select the destination for the unique Order ID in the database table.
6 If you want to redirect the user after the data is submitted, enter the path in the Go to URL field or click Browse to select the page.
7 Click OK when you are finished.
Note: The data source to which you are submitting must be a view that is updatable in your database schema. If you have a view that contains a join and your database application does not allow inserting into a view with a join, you will receive errors.

To Table of Contents Back to Previous document Forward to next document