Add products to orders

This section handles the page for adding new products to an existing order. Due to the structure of this application however, this page will also perform other tasks (like initiating the order). Creating a new order means adding elements into two database tables: the order's main properties go into the order_ord table (the ID, the date and state, as well as the session ID), while the correspondence to the products it contains is made through the order_product_opr table (which stores pairs of ID's: order - product).

When a user enters the site, after logging in, there is no existing order into which he can add the products. Therefore one must be created, that is unique and tied to the current browsing session. To achieve this, the session ID is used as a property for the order. The mechanism that takes place on the add_to_order page is as follows:

  1. After a user clicks the Add to order link for a product, the add_to_order page is loaded with the product's ID as URL parameter.

  2. The page code must check if an order associated to this session already exist, by verifying if an order ID is stored in the session variable kt_order_id. If found, it is the ID of the order that will be used throughout the insert operations into the order_product_opr table.

  3. If an existing order is not found with the ID in the kt_order_id session variable, the session ID is retrieved, and a table search is conducted, to see if an existing order exists for this session, but is not loaded in the kt_order_id variable. If one is found, it's ID is retrieved and loaded into a session variable (the one checked at step 2) and the insert transaction carries on.

  4. If no matching record is found in the database, it means that no order was ever associated to this user's session. Therefore, a new order is created by inserting into the order_ord table a new record, with the present date, the initiated state (the 0 value) and with the current user's ID. Then, the last inserted ID is also saved into a session variable.

 

All the actions described above take place before the actual insert operation that adds a new product to a order, and they cannot be done automatically by MX Kollection. Some custom code is necessary to achieve this set of operations. MX Kollection helps however through the use of its Custom Trigger server behavior, that allows entering code and deciding when to execute it. Start by opening the add_to_order page in Dreamweaver.

Before you can add a custom trigger on the page, you must have a transaction (either Insert, Update, Delete or Custom). Since the page's purpose is to insert products into the order, you will apply the Insert Record Form Wizard. You can access it from the MX Kollection tab of the Insert bar and must configure it as follows:

  1. In the wizard's first step, select the database connection created at the beginning of this tutorial. For the table to insert into, use order_product_orp (it is the linking table, that only stores pairs of ID's and the quantity). For the redirect page, use the product list (view_products):


     

  2. In the second step, you must decide what fields will be used from the existing table columns. Configure them as follows:

  3. The third step of the wizard allows you to define validation rules for each field involved in the transaction. You can safely skip this step and close the wizard by pressing the Finish button.

 

At this point the page contains the form allowing to insert records into the database (as HTML elements) and the Insert transaction (regarding the logic). Since a transaction exists, this means you can add triggers to the page. The Custom Trigger is the most useful at this point.

To add a custom trigger to the page, access it from Server Behaviors > MX Kollection > Forms > Custom trigger. In the user interface that opens, you can enter code. The code executing the operations mentioned on top of this page is provided below:

 


 

Next, you have to set the trigger to be executed before the transaction. To set trigger properties, click on the Advanced tab. Here, set the Type from the drop-down menu to BEFORE and the priority to 1. This means that the trigger will execute before the transaction it is registered to (you can see it in the grid), with the highest priority.

 

 

Now, when the page loads, the existence of an order for the current session is checked, and if it does not exist, one is initialized, before the user actually adds the product and the transaction will go on.

After adding the possibility to browse the product list and add any of them to the order, together with a specified quantity, you must create the pages that allow finalizing the order. To create them, move on to the next section of the tutorial.