Template Example: Donations

Though CartThrob isn’t a solution like OSCommerce where templates can be simply swapped out, it’s helpful to have somewhere to start. The template example below is meant to be a guide, and will give you somewhere to start building a site that takes donations.

This template code is also included with CartThrob as one of the auto-install templates. This and other example templates can be automatically installed, along with sample weblogs and settings by going to the CartThrob extnsions settings page: Global Settings > Auto-Install Templates. This specific example is contained in the “cart_donations” template group of the auto-install templates.

{examples_channel_configuration}

{/examples_channel_configuration}
{!-- You should almost always secure your pages to take transactions. To secure your pages use the following tag, to load your page as an HTTPS connection: {exp:cartthrob:https_redirect} This tag is currently commented out, because this page would fail to load if you have not yet installed your security certificate. If you need to know more about security certificates, please feel free to ask questions in our forums: http:cartthrob.com/forums --}
{assign_variable:template="cart_donations"}
{!-- DELETE A PRODUCT --}
{if segment_2=="delete"}
{!-- The delete_from_cart tag deletes items. 
	In this case it is only called if segment_2 is "delete"
	if used on a page with other tags, place it towards the top of the page. 
	 --}
	{exp:cartthrob:delete_from_cart delete_all="yes" row_id="{segment_3}"}
{/if}
{embed=includes/.header title="Taking Donations" }

</head>
<body>
	
	<h1>Taking Donations</h1>
	<p>This page shows an example of how to take donations of any amount</p>
	{!-- ORDER COMPLETE MESSAGES --}
	{!-- The "return" paramater of the checkout form below is set back to this page with "order_complete" in the URL. 
		This saves creating a template specifically to handle order info. --}
	{if segment_2=="order_complete"}
		{!-- the submitted_order_info tag returns information from the last attempted order. --}
		{exp:cartthrob:submitted_order_info}
		    <div class="store_block">
				{if authorized}
					Your transaction is complete!<br />
			        Transaction ID: {transaction_id}<br />
			        Your total: {cart_total}<br />
			        Your order ID: {order_id}
			    {if:elseif declined}
			        Your credit card was declined: {error_message}
			    {if:elseif failed}
			        Your payment failed: {error_message}
			    {/if}
			</div>
		{/exp:cartthrob:submitted_order_info}
	{/if}

	{!-- ADD A PRODUCT --}

    <div class="store_block">
	<h2>Make a Donation</h2>
	{exp:cartthrob:add_to_cart_form 
	    return="{template}/index" 
		allow_user_price="yes"
		title="Donation"
		no_shipping="yes"
		no_tax="yes"
	    on_the_fly="true"  
		}
		<p>
			
			Donation Amount:  $<input type="text" maxlength="7" size="5" name="price"> 
			{!-- Adding a personal_message to the donation. No field called personal_message exists,
				but if you are using the "Cartthrob Order Items" custom field type in your Orders Weblog… 
				this message will still be dynamically added to the order data. 
				See the add_to_cart_form for more details
				 --}
			Donation Note: {item_options:input:personal_message value="" }<br />
		</p>
	    <input type="submit" value="Submit" />
	{/exp:cartthrob:add_to_cart_form}
	</div>


	{!-- VIEW CART CONTENTS / UPDATE QUANTITIES --}
	<div class="store_block">
		<h2>Cart Contents</h2>
		
	{!-- cart_items_info outputs information about your current cart, including products in the cart, weight, and prices. --}
	{exp:cartthrob:cart_items_info}
		{if no_results}
		<p>Your cart is empty</p>
		{/if}
		{!-- outputting data that's only applicable for the first item. --}
		{if first_row}
			<h2>Thank You.</h2>
			<p>Thank you for your donation commitment, please pay for your donation now.</p>
		{/if}
		<p>Title: {title} <br />

			Personal Message: {item_options:personal_message}<br />
			{!-- The delete URL links back to this page. 
			The segments activate the delete_from_cart tag at the top of this template.--}
			<a href="{path={template}/delete/{row_id}}">Delete</a><br />
			</p>
		
		{if last_row}
			{!-- these are just some of the variables available within the cart_items_info tag --}
			<p><strong>Total: {cart_total}</strong></p>
		{/if}
	{/exp:cartthrob:cart_items_info}
	</div>

	{!-- CHECKOUT --}
	<div class="store_block">
	<h2>Checkout</h2>
	{!-- the checkout_form  outputs a checkout form--}
	{!-- overriding the chosen gateway with the the dev_template gateway here --}
	{exp:cartthrob:checkout_form gateway="dev_template" return="{template}/order_complete"}
		{!-- The gateway_fields template variable to output fields required by your currently selected gateway 
			what you see on the front end changes based on the gateway's requirements.--}
		{gateway_fields}
		<input type="submit" value="Checkout" />
	{/exp:cartthrob:checkout_form}
	</div>
	
	<div class="store_block">
		<h2>Tags used in this template</h2>
		<ul>
			<li><a href="http://cartthrob.com/docs/tags_detail/add_to_cart_form">add_to_cart_form</a></li>
			<li><a href="http://cartthrob.com/docs/tags_detail/cart_items_info">cart_items_info</a></li>
			<li><a href="http://cartthrob.com/docs/tags_detail/checkout_form">checkout_form</a></li>
			<li><a href="http://cartthrob.com/docs/tags_detail/delete_from_cart">delete_from_cart</a></li>
			<li><a href="http://cartthrob.com/docs/tags_detail/submitted_order_info">submitted_order_info</a></li>
			<li><a href="http://cartthrob.com/docs/tags_detail/update_cart_form">update_cart_form</a></li>
		</ul>
		
		<h2>Concepts used in this template</h2>
		<ul>
			<li><a href="http://cartthrob.com/docs/sub_pages/purchased_items_overview">Purchased Items Weblog</a></li>
			<li><a href="http://cartthrob.com/docs/sub_pages/orders_overview">Orders Weblog</a></li>
		</ul>
	</div>
	<div class="store_block">
		{embed=includes/.footer}
	</div>

</body>
</html>

The easiest method for using this template is as part of CartThrob’s built-in auto-install templates system, but is included here as an overall sample of what some of the tags can do when put together.

Top of Page