Weights and Carriage
Previous  Top  Next

This page does not apply to the Lite version of the cart.

The cart has the ability to calculate the total order weight by multiplying each items individual weight and multiplying this by the quantity ordered, this value is accessible vie the carts TotalWeightGr, TotalWeightKg and TotalWeightOz properties, the items individual weight must be included when the item is added to the cart and must be passed as grams, of you don't pass it in as grams the totals returned by the mentioned properties will be incorrect.

We will now modify the site you have created during this walkthrough to support calculating the carriage costs based on the weight of the items ordered as many carriage companies calculate costs based on consignment weight.

Begin by adding a "Weight" field to the tblProducts table in the database, the "data type" should be set to "number" and the "Field Size" should be set to "Double", this is shown in the following image:

clip0099

Next run the table in Access and input the weight value in grams for each product as shown in the following image:

clip0100

After you have defined the weight value for all the products you will have to add the weight field to the "web_Products" query, select the query in Access and click the clip0102 button and then add the Weight field to the query as shown in the following image:

clip0101

Once done click the clip0103 button and the query will run, verify the Weight field is there and looks similar to the following image:

clip0104

If its working close this window and say "yes" to saving the query, that's the database side complete so you can close it now.

Now we will modify all the page/s to include the Weight when adding items to the cart.

The first page we will edit is "products/AddFromDB.aspx", edit the "Add To Cart From Database" control clip0009 add the Weight field to the Field Mappings as shown in the following dialog image:

clip0105


Save this page and then do precisely the same procedure on the "Add To Cart From Database" control found in the "products/BatchAdd.aspx" page.

If you have already set-up the "products/multiple.aspx" page as outlined in the Multiple Additions section then you will have to add an additional "webxelcart:formfield" control to the field collection. First of all you will have to refresh the dataset so that the Weight field appears in the bindings panel field list, do this by double clicking the dataset and then clicking the "OK" button, this will re-apply the dataset and make the Weight field visible. Then you should insert the new webxelcart:formfield control by clicking the clip0012 icon and defining the following values in the FormField dialog

FieldName = Weight  
FieldValue = <%# DataSet1.FieldValue("Weight", Container) %>  
FieldType = hidden  
 
Once this is done all that's left is to modify the code that calculates the carriage in the Order Summery page (cart/summery.aspx).

If you recall this code looks like this:

C#  
<script runat="server">  
void Cart1_Load(object sender, System.EventArgs e)  
{  
if(Cart1.GrandTotal > 50){Cart1.CarriageCost = 0;}  
else{Cart1.CarriageCost = 15;}  
}  
</script>  
 
VB  
<script runat="server">  
Sub Cart1_Load(sender As Object, e As System.EventArgs)  
If Cart1.GrandTotal > 50 Then  
Cart1.CarriageCost = 0  
Else  
Cart1.CarriageCost = 15  
End If  
End Sub  
</script>  

We need to change it so that the carriage is calculated based on the carts TotalWeightKg property, make the modification shown in blue text


C#  
<script runat="server">  
void Cart1_Load(object sender, System.EventArgs e)  
{  
Cart1.CarriageCost = Cart1.TotalWeightKg * 1.50M;  
}  
</script>  
 
VB  
<script runat="server">  
Sub Cart1_Load(sender As Object, e As System.EventArgs)  
Cart1.CarriageCost = Cart1.TotalWeightKg * 1.50  
End Sub  
</script>  

This code assumes you wish to charge 1.50 per Kg for carriage cost, if you need to be more precise you can use the carts TotalWeightGr property to calculate costs on a per gram basis, carriage companies normally work in Kg though.

Whats the 1.50M
all about?
In C# any literal value that's to be treated as a decimal value must have M on the end to tell C# it's a decimal and not some other type of data.