Page Templates:Creating a Shopping Cart Template

From Whirlwind eCommerce Wiki
Jump to: navigation, search

Create shopping cart template

Below is an example shipping cart template:

// shopping cart 
 
// get our order info
$completeOrderArray = eV::orderGetCompleteArray($_SESSION['order']['orderId']);
 
// shortcut the assoc arrays returned, easier to reference later
$orderArr = $completeOrderArray['order'];
$itemArr = $completeOrderArray['items'];
$discountArr = $completeOrderArray['discounts'];
// payment info not usually shown in cart, but for the sake of this example ill shortcut it also
$paymentArr = $completeOrderArray['payments'];
 
// get shipping information to be used in shipping selection
// first set a blank array
$shipArr = array();
// than populate the array onlyy if items are in the cart
// this will error out with no items in cart as the attempt...
// ... to get the zip code from an invalid item index will error
// note we pass the zip code from the first item  - ship addresses are tied to items
// note we get the weightTotal from the orderArr
 
// below is a sample call if we are using zip, weight based shipping:
if(!empty($itemArr)) $shipArr = eV::orderGetShipArray(NULL,$itemArr[0]['zipCode'],$orderArr['weightTotal']);
 
// below is a smple call if we are using shipping levels
// which are $ based, not ship / weight based
if(!empty($itemArr)) $shipArr = eV::orderGetShipArray($orderArr['itemTotal']);
 
 
// link to continue checkout process
// note we only show it if items are in the cart. No reason to checkout on empty cart
// note we are using the redirectIfRegistered attribute to have user skip this page if they are registered (logged in) and go directly to the checkoutShipping page
if(count($itemArr) > 0) echo "<a href=\"index.php?fa=ORDER.register&redirectIfRegistered=" . rawurlencode('index.php?fa=ORDER.checkoutShipping') . "\">CONTINUE CHECKOUT</a>";
 
 
// start the cart form
echo "<form action=\"index.php\" method=\"post\" name=\"cartForm\">";
// hidden attrib - rrquired for cart form, tells it what to do when submitted
echo "<input type=\"hidden\" name=\"fa\" value=\"ORDER.cartUpdate\">";
 
// display any messages passed into the cart
// this will show notification messages to the shopper, such as when they add discounts to the cart, or a discount gets dropped because they no longer qualify, etc...
if(!empty($message)) echo "<hr>$message";
 
// drop a message that the cart is empty if nothing is in it
if(count($itemArr) == 0) {
	echo "<hr>Your cart is empty";
} else {
	// something in it? well lets let visitor know what we are about to display
	echo "<hr>Cart Items:";
};
 
// alrighty lets loop thru items in cart and display them
foreach($itemArr as $item){
	// link back to product
	// note we can pass the productId in the URL and the system will find the first page that contains that product
    echo "<br><a href=\"index.php?productId=" . $item['productId'] .  "\">" . $item['productName'] . "</a>";
 
	// display the cart description, only if it has value
 	if(!empty($item['cartDescription'])) echo "<br>" . $item['cartDescription'];
 
	// does this item have options? lets spit em out then....
	if(count($item['options']) > 0){
		echo "<br>";
		// loop through the options
		foreach($item['options'] as $option){
			// display the option
			echo $option['groupName'] . ": " .$option['title'] . " ";
			// if item has a dollar value associated with it, lets show that also...
			// the dollarFormat function is a quick shortcut to turn an int/float into a dollar formatted output
			if(floatval($option['price']) > 0) echo "(+" . eV::dollarFormat($option['price']) . ") ";
		// end looping through options
		};
	// end if we have options
	};
 
	// display the item sku
	// the item sku is the full appended sku including the productNumber + all option skus
	echo "<br>Item #: " . $item['sku'];
	// quantity field
	// note the name is quantity# where # is the itemId
	// This tells the recieving script what quantity to change
	echo "<input name=\"quantity" . $item['itemId'] . "\" value=\"" . $item['quantity'] . "\" type=\"text\" size=\"5\" />";
	// update link
	// this link just submits the form
	// all quantities will be updates upon form submittion
 	// the single update function tries to update quantities, discounts and shipping selection
	echo "<a href=\"javascript:window.document.cartForm.submit();\">UPDATE</a> ";
 
	// remove link
	// this does the same thing as submitting the quantity form...
	// except it instructs one item quantity to be set to 0...
	// which the system will use as a queue to drop the item all together
	// note that other item's quantities are not passed here...
	// if not passed, the system will leave thier quantity as is
	echo "<a href=\"index.php?fa=ORDER.cartUpdate&quantity" . $item['itemId'] . "=0\">REMOVE</a> ";
	// output the price per item and the total price (quantity x pricePer)
	echo "x " . eV::dollarFormat($item['pricePer']) . " = " . eV::dollarFormat(($item['pricePer'] * $item['quantity']));
 
// end looping through items
};
 
// merchandise total
echo "<hr>TOTAL:" .  eV::dollarFormat($orderArr['itemTotal']);
 
// add a discount/coupon form field
echo "<hr>Enter Coupon Here: <input name=\"discountCode\" type=\"text\" size=\"15\" /> ";
 
// link to post discount
// note this link submits the cart form...
// this means that all quantities will be updated as well based on entries above
// and vice-verse if a discount is entered and the update is clicked for a quantity...
// the system will attempt to apply the discount
// the single update function tries to update quantities, discounts and shipping selection
echo "<a href=\"javascript:window.document.cartForm.submit();\">APPLY</a>";
 
// display the discounts
// loop thru discount array
foreach ($discountArr as $disc){
 
	// discount the discount code
	// this is the same code entered by the user to apply the discount
	echo "<br>Discount: " . $disc['discountCode'];
 
	// display a link to remove the discount
	// this is necessary as some discounts will not work with other discounts in the cart
	// also admins can set the carts to only allow one discount per order in Settings
	echo " <a href=\"index.php?fa=ORDER.discountRemove&discountId=" . $disc['discountId'] . "\">remove</a> ";
 
	// discount description
	echo $disc['description'];
 
	// discount value
	// note we add the negative "-" sign
	// as all discount values are in the array as positive numbers
	echo " -" . eV::dollarFormat($disc['amount']);
 
// end loop through discounts
};
 
// shipping options
// lets only display if there are items in the cart
if(count($itemArr) > 0){
 
	echo "<hr>Select Shipping Option: ";
	// shipping option select field
	// note the name of the field "shipping4addressId#...
	// .. this is to support multiple shipping addresses with different ship methods...
	// .. a future function
	// also note that the address and shipping data are attached to the item(s)
	// we call on the sale_addressId from the first item since we are not using multi-shipto-addresses
	// only need this field if multiple shipping options for visitor, otherwise don't include
	echo "<select name=\"shipping4addressId" . $itemArr[0]['sale_addressId'] . "\">";
	// loop thru each shipping option and display as an option
		for($i=0;$i<count($shipArr);$i++){
		// start the option box, set the typeCode as the value
		echo "<option value=\"" . $shipArr[$i]['typeCode'] . "\"";
		// if this matches the current ship method selected by user, than select this option
		if($shipArr[$i]['typeCode'] == $itemArr[0]['shipTypeCode']) echo " selected ";
		// complete the option box, display the full ship type and it's cost
		echo ">" . $shipArr[$i]['type'] . " : " . eV::dollarFormat($shipArr[$i]['shipAmount']) . "</option>";
	// end loop through shipping options
	};
 
	// close out the shipping select field
	echo "\n</select>";
 
	// zip code entry field for shipping
	echo " Enter Zip Code: ";
	// note we popoulate it with the zipCode from the first item in the cart
	// only need zipCode here if using zip based shipping, like fedex
	// level shipping is not zip code based, therefore dont need this field
	echo "<input type=\"text\" name=\"zipCode\" value=\"" . $itemArr[0]['zipCode'] . "\" size=\"12\" maxlength=\"10\"> ";
	// update link
	// as before, updating the form will post changes to shipping, add discount and post changes to quantities
	echo "<a href=\"javascript:window.document.cartForm.submit();\">update</a>";
	// current shipping charge
	echo " Shipping: " . eV::dollarFormat($orderArr['shippingTotal']);
 
	// lets show the total due
	echo "<hr>Total: " . eV::dollarFormat($orderArr['totalDue']);
 
// end if items in cart
};
 
// repeat link to continue checkout process
// note we only show it if items are in the cart. No reason to checkout on empty cart
// note we are using the redirectIfRegistered attribute to have user skip this page if they are registered (logged in) and go directly to the checkoutShipping page
if(count($itemArr) > 0) echo "<hr><a href=\"index.php?fa=ORDER.register&redirectIfRegistered=" . rawurlencode('index.php?fa=ORDER.checkoutShipping') . "\">CONTINUE CHECKOUT</a>";
 
// close out the form
echo "</form>";

Create shopping cart page

assign the new cart template to this page
check the 'hide this page from in-site searches'
post this page and take note of the page Id

Set new page as shopping cart

In settings > eCommerce tab
enter the pageId for the page just created into the "cart page id" field.
post changes to settings