Page Templates:Creating a Shipping Address Template

From Whirlwind eCommerce Wiki
Jump to: navigation, search
Page Template Managers

The shipping address template is a form for the user to enter their shipping address.

The billing address and shipping address templates are virtually identical. Note that all the fields in the the billing address form refer to the payment array, while the fields in the shipping address form refer to the item array (where the shipping address resides).

Also be sure the redirect success and redirect failure attributes (hidden variables at end of form) are pointed correctly. Unless otherwise required, the redirect failure attribute should point back into the current page (on submission failure, such as invalid or missing information, the visitor will be sent to the redirect failure location and provided with a message detailing the reason for the failure). The redirect success attribute should be the next step in the checkout process - if shipping address is before billing address, then send redirect success to index.php?fa=ORDER.checkoutBilling - if shipping is after billing but before payment, then send redirect success to index.php?fa=ORDER.checkoutPayment.

After creating the template, setup a page for it. Place the pageId of this new page in Settings > eCommerce tab: "Shipping Address Page Id"

Sample content is below:


// when called correctly (using fa=ORDER.checkoutShipping)
// the $addressArr array will be passed to this page 
// containing all the fields for the address form
// also $orderArray will be passed with the complete cart data
 
// if using the address book, we need to get the user's address
if($isLoggedIn) $arrAddresses = eV::userGetAddresses("emailAddress,firstName,lastName,company,street1,street2,city,state,zipCode,country,phone1,addressName,addressId");
 
// get the options for the state select field
// pass addressArr['state'] to set the detauly
$stateOptionString = eV::addressStateOptions($addressArr['state']);
 
// same as state but for country
// only if client requires country selection in address forms
$countryOptionString = eV::addressCountryOptions($addressArr['country']);
 
 
// get the billing address from orderArray
// only necessary if using the 'use this address for billing' option, before billing page
// or if we are using the 'use my billing address' option, after billing page
// pulling from address in first payment
$billingAddressId = $orderArray['payments'][0]['sale_addressId'];
 
// display message if there is one
// this variable will be uysed by the system
// to report back any incomplete/ invalid information in the form
if(!empty($message)) echo "<p><font color=red><b>$message</b></font></p>";    
 
// output content management controlled data for the page, if necessary
echo "$title
<br>$subTitle
<br>$copy";
 
 
// display address selections if logged in an using address book
if($isLoggedIn) {
	// here's a nifty javascript function 
	// to allow selection of addresses auto populate the form
	// make sure to adjust to your specific fields!
	echo "
	<script language=\"javascript\">
	<!--
	function popAddress(firstName,lastName,street1,street2,city,state,zipCode,phone1,emailAddress,company){
		formObj = window.document.shippingAddressForm;
		formObj.firstName.value = firstName;
		formObj.lastName.value = lastName;
		formObj.street1.value = street1;
		formObj.street2.value = street2;
		formObj.city.value = city;
		formObj.zipCode.value = zipCode;
		formObj.phone1.value = phone1;
		formObj.emailAddress.value = emailAddress;
		formObj.company.value = company;
		for(var i=0;i<formObj.state.length;i++) if(formObj.state.options[i].value == state) formObj.state.selectedIndex = i; 
	};
	// -->
	</script>
	";
 
	// loop thru available addresses
	// $arrAddresses is an array returned by the call to userGetAddresses at beginning
	foreach($arrAddresses AS $row){
		// output address data
		// you can show as little or as much info here as you would like
		echo "<hr>" . $row['firstName'] . " " . $row['lastName'] . "
			<br>" . $row['street1'] . "
			<br>" . $row['street2'] . "
			<br>" . $row['city'] . ", " . $row['state'] . " " . $row['zipCode'];
 
		// create the javascript call to select the address
		// uses the popAddress function created above
		echo "<br><a href=\"javascript:popAddress('" . rawurlencode($row['firstName']). "','" . rawurlencode($row['lastName']). "','" . rawurlencode($row['street1']). "','" . rawurlencode($row['street2']). "','" . rawurlencode($row['city']). "','" . rawurlencode($row['state']). "','" . rawurlencode($row['zipCode']). "','" . rawurlencode($row['phone1']). "','" . rawurlencode($row['emailAddress']). "','" . rawurlencode($row['company']) . "');\">SELECT</a>";
 
	// end arrAddresses loop
	};
 
// end if logged in
};
 
// start address form
// note use securedSiteUrl attribute - this is set in Settings > Site Tab. Use to ensure we go to a secured page
echo "<form name=\"shippingAddressForm\" action=\"" . $iProducts['securedSiteUrl']. "/index.php\" method=\"post\">";
 
 
// heres a handy feature - use this checkbox to dupe the address from billing
// if checked visitor does not need to fill out shipping address form (it will be ignored)
// note this checkbox should only be used if shipping address is AFTER billing address in the checkout process
// if using this checkbox, than be sure to remove the copyTo_sale_addressId checkbox at the end of this form...
// ... as if this checkbox is usable (meaning shipping address is after billing address), than the...
// ... copyTo checkbox is not usable (because it cant be used if this is after billing address)
echo "<input name=\"copyFrom_sale_addressId\" class=\"checkbox\" type=\"checkbox\" value=\"$billingAddressId\" /> Same as billing address";
 
 
// now for each of the fields
// note the use of $addressArr array which contains all the values
// $addressArr array is autotically available to the template when called correctly (fa=ORDER.checkoutShipping)
echo "<br>Email Address: <input name=\"emailAddress\" type=\"text\" value=\"" . $addressArr['emailAddress'] . "\" size=\"30\" maxlength=\"50\"/>";
 
// if you choose to use the subcribe checkbox
// and a visitor selects it
// they will be subscribed to the newsletter automatically
// just make sure the input is names "subscribe"
// most clients want it always defaulted to checked
echo  "<input name=\"subscribe\" type=\"checkbox\" value=\"1\" checked=\"checked\"/> Check to Subscribe";
 
echo "<br>First Name: <input name=\"firstName\" type=\"text\" value=\"" . $addressArr['firstName'] . "\" size=\"30\" maxlength=\"50\"/>";
echo "<br>Last Name: <input name=\"lastName\" type=\"text\" value=\"" . $addressArr['lastName'] . "\" size=\"30\" maxlength=\"50\"/>";
echo "<br>Company Name: <input name=\"company\" value=\"" . $addressArr['company'] . "\" type=\"text\" /size=\"30\" maxlength=\"50\">";
echo "<br>Street 1: <input name=\"street1\" type=\"text\" value=\"" . $addressArr['street1'] . "\" size=\"30\" maxlength=\"50\"/>";
echo "<br>Street 2: <input name=\"street2\" type=\"text\" value=\"" . $addressArr['street2'] . "\" size=\"30\" maxlength=\"50\"/>";
echo "<br>City: <input name=\"city\" type=\"text\" value=\"" . $addressArr['city'] . "\" size=\"30\" maxlength=\"50\"/>";
 
// note state is a select input
// use the stateOptionString created at beginning to easily output state options
echo "<br>State:
		<select name=\"state\">
                $stateOptionString
		</select>";
 
echo "<br>Zip/Postal Code: <input name=\"zipCode\" type=\"text\" value=\"" . $addressArr['zipCode'] . "\" size=\"12\" maxlength=\"12\"/>";
 
// note country is a select input
// use the countryOptionString created at beginning to easily output country options
echo "<br>Country:
		<select name=\"country\">
                $countryOptionString
		</select>";
 
echo "<br>Phone Number: <input name=\"phone1\" type=\"text\" value=\"" . $addressArr['phone1'] . "\" size=\"25\" maxlength=\"25\"/>";
 
// heres a handy feature - use this checkbox to dupe the address to billing opon submission
// note if billing address is before billing in the process, use the checkbox in billing instead
// note we are using billingAddressId which we created beginning of this script from orderArray
// only use this checkbox if the shipping address comes before the billing address in the checkout process...
// ... as it will not work if the shipping address is after the billing address
// If using this checkbox, be sure not remove the copyFrom_sale_addressId checkbox at beginning of this form
echo "<input name=\"copyTo_sale_addressId\" type=\"checkbox\" value=\"$billingAddressId\" onChange=\"toggleRedirectSuccess(window.document.billingAddressForm.copyTo_sale_addressId.checked);\"/> 
       Use same Shipping Address as your Billing Address";
 
// the following javascript function is handy to change the success target page
// this will skip the shipping address form if checked
echo "
	   <script language=\"javascript\">
			<!--
			function toggleRedirectSuccess(copy2Billing){
				var fieldObj = window.document.shippingAddressForm.redirectSuccess;
				if(copy2Billing) fieldObj.value = 'index.php?fa=ORDER.checkoutPayment&hideKeyField=1';
				if(!copy2Billing) fieldObj.value = 'index.php?fa=ORDER.checkoutBilling&hideKeyField=1';
			};
			// -->
		</script>
 
";
 
// submit button to submit the address form
echo "<input type=\"submit\" value=\"continue\">";
 
// required hidden fields for the address form to work
// note the redirectSuccess, redirectFailure fields to tell the system where to go after the form is processed
// redirectOnFailure will almost always loop back into this form
// redirectOnSuccess will go to the next step in the process, which is usually checkoutShipping, unless shipping is after billing than send them to ORDER.checkoutPayment
echo "
<input type=\"hidden\" name=\"fa\" value=\"ORDER.checkoutShipping\">
<input type=\"hidden\" name=\"formAction\" value=\"form\">
<input type=\"hidden\" name=\"addressId\" value=\"" . $addressArr['addressId'] . "\">
<input type=\"hidden\" name=\"redirectSuccess\" value=\"index.php?fa=ORDER.checkoutBilling&hideKeyField=1\">
<input type=\"hidden\" name=\"redirectFailure\" value=\"index.php?fa=ORDER.checkoutShipping&hideKeyField=1\">
";
 
// the following are optional fields that dictate what fields are required and what message is returned to the visitor if they fail to provide the field information
// without specifying these fields, the default required fields are: firstName,lastName,street1,city,state,zipCode,phone1,emailAddress
echo 
"
<input type=\"hidden\" name=\"requiredFieldList\" value=\"firstName,lastName,street1,city,state,zipCode,phone1\">
<input type=\"hidden\" name=\"requiredFieldDescList\" value=\"First Name is required,Last Name is required,Address 1 is required, City/town is required,State is required,Zip is required,Daytime Phone is required\">
";
 
// wrap up our form
echo "</form>";