Shipping

thewojogroup edited this page Sep 13, 2010 · 22 revisions

There are several methods for adding a shipping cost to your cart. First, you can setup some basic shipping calculation for the entire cart:


 /**** add a flat shipping rate to the entire order ****/
 simpleCart.shippingFlatRate = 10.00;
 /**** add a shipping cost of 3.00 for every item in the cart ****/
 simpleCart.shippingQuantityRate = 3.00;
 /**** add a shipping cost as a percentage of the total cost ****/
 simpleCart.shippingTotalRate = 0.05;

You can implement any combination of these shipping options and they will add together for the total cost.

Individual Item Shipping


simpleCart(js) allows you to set a shipping field for an item that will add a special shipping cost for that item. If you are using the Shelf ( Shelf Setup ) you can add a hidden field for an item:
  <div class="simpleCart_ShelfItem">
   <h2 class="item_name">T-shirt</h2>
   <span class="item_price">$15.00</span>
   <input type="text" class="item_quantity" value="1" />
   <!-- field for item shipping cost -->
   <input type="hidden" class="item_shipping" value="5.00" />
   <a href="javascript:;" class="item_add">add to cart</a></div>

If you are using the ‘add’ function for adding items to the cart:


  onclick="simpleCart.add('name=T-shirt','price=15.00','quantity=1','shipping=5.00');"

Advanced Shipping Functions

If you require some advanced calculations for shipping, you can use the prototype object for CartItems:


  CartItem.prototype.shipping=function(){
   // we are using a 'size' field to calculate the shipping,
   // so we first make sure the item has a size
   if(this.size){
    if( this.size == 'small' ){
     return this.quantity*5.00;
    } else if( this.size == 'large') {
     return this.quantity*7.50;
    } else {
     return this.quantity*10.00;
    }
   } else {
    // use a default of $2.00 per item if there is no 'size' field 
    return this.quantity*2.00;
   }
  }

The ‘this’ here refers to the item, and we are using a ‘size’ field and the item ‘quantity’ to calculate the shipping. Because an item may or may not have a size, we check to make sure it does with the ‘if(this.size)’. If there is a size field, we use a different rate for each size and return a value based on the item quantity. Otherwise, we use a base rate of 2.00. simpleCart(js) will use the global shipping rates, and then add the shipping value for each item in the cart.

If you set a shipping field in the Shelf Item or when adding, it will override this function method. You can use a combination of all these methods to create some very advanced shipping calculations.