Template Example: Software Downloads

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 sells downloads

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_software” template group of the auto-install templates.

{examples_channel_configuration}

{/examples_channel_configuration}
{if segment_2=="download_file"}{exp:cartthrob:download_file encrypted="yes" file="{segment_3}" member_id="{segment_4}" }{/if}
{!-- it's usually a good idea to put a download_file link on its own page at the very top because nothing else is output when a download link is activated. 
	for this demo everything we need is placed on the same template to make it easier to see everything at once. 
	The view_download_link tag automatically encrypts content, so if you use that to generate your download links, you should always set encrypted to true/yes/1.  
--}
{!-- 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_software"}
{embed=includes/.header title="Software Store" }

</head>
<body>
	{!-- DELETE A PRODUCT --}
	{if segment_2=="delete"}
	{!-- The delete_from_cart tag deletes a product. 
		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}
	
	<h1>Software Store</h1>
	<p>This page shows an example of how you can sell & protect your downloadable products</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}

	<div class="store_block">
		<h2>Your Previous Software Purchases</h2>
		{exp:weblog:entries weblog="purchased_items" author="CURRENT_USER" search:purchased_product_download_url="not IS_EMPTY"}
			{if no_results}
				<h3>You have not purchased any software, but here's a link anyway</h3>
				{!-- this is where you would add a link to the file the customer just purchased 
				to keep things simple, i've just added a link to a file that ships with CartThrob so you can test the download & encryption. 
				You can save information about individual items in a purchased_items weblog, or complete orders in an orders weblog.
				--}
				<a href="{exp:cartthrob:view_download_link 
					template='{path={template}/download_file}' 
					file ='{path=themes/cp_themes/default/cartthrob/images/cartthrob_logo_bg.jpg}' 
					member_id='{logged_in_member_id}'}">Download</a>
				<br />
			{/if}
			
			<a href="{exp:cartthrob:view_download_link 
				template='{path={template}/download_file}' 
				file ='{path=images/uploads/{purchased_product_download_url}}' 
				member_id='{logged_in_member_id}'}"><strong>Download</strong> <em>{title}</em> </a><br />
		{/exp:weblog:entries}
	</div>	
	{!-- ADD A PRODUCT --}
    <div class="store_block">
	<h2>Add Products</h2>
	{!-- outputting products stored in one of the "products" weblogs. These are exactly the same as normal 
		product weblogs, so the weblog names may be different from what is listed below --}
	{exp:weblog:entries weblog="products" limit="10"}
		{!-- The add_to_cart_form adds 1 or more of a product to the cart --}
		{!-- checking to see if this product has any content in the product_download_url custom field --}
		{if product_download_url}
			{exp:cartthrob:add_to_cart_form 
				entry_id="{entry_id}" 
				return="{template}/index"}
					<p>Product name: {title}<br />
					Price: ${product_price}<br />
					Quantity: <input type="text" name="quantity" size="5" value="" /><br />
					<input type="hidden" name="item_options[purchased_product_download_url]" value="{product_download_url}" />
					{!-- this field gathers information for the purchased items weblog's custom field call purchased_product_download_url 
						If you do something similar, you can name the field anything you want, 
						as long as you add the field name as a key to the item options input field  --}
				
					<input type="submit" value="Add to Cart">
					</p>
			{/exp:cartthrob:add_to_cart_form}
		{/if}
	{/exp:weblog:entries}
	</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>There is nothing in your cart</p>
		{/if}
		{!-- outputting data that's only applicable for the first item. --}
		{if first_row}
			    <table>
			        <thead>
			            <tr>
			                <td>Item</td>
			                <td colspan="2">Quantity</td>
			            </tr>
			        </thead>
			        <tbody>
		{/if}
			        <tr>
		                <td>{title}</td>
		                <td>
		                </td>
		                <td>
							{!-- This 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>
		                </td>
		            </tr>
		{if last_row}
		{!-- outputting data that's only applicable for the last item. --}
			            <tr>
			                <td>
								{!-- these are just some of the variables available within the cart_items_info tag --}
			                    <p>Subtotal: {cart_subtotal}<br />
			                    Shipping: {cart_shipping}<br />
			                    Tax: {cart_tax}<br /> 
								{!--tax is updated based on user's location. To create a default tax price, set a default tax region in the backend --}
			                    <strong>Total: {cart_total}</strong></p>
								<p>
								{!-- total quantity of all items in cart --}
								Total Items: {exp:cartthrob:total_items_count}<br />
								{!-- total items in cart --}
								Total Unique Items: {exp:cartthrob:unique_items_count}</p>
			                </td>
			                <td colspan="2"> </td>
			
			            </tr>
			        </tbody>
			    </table>
			    <input type="submit" value="Update Cart" />
		{/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/download_file">download_file</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>
			<li><a href="http://cartthrob.com/docs/tags_detail/view_download_link">view_download_link</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>
		</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