CartThrob 10 documentation for ExpressionEngine commerce teams Visit CartThrob.com
CartThrob.com

Template Example: Software Downloads

This example shows the safe shape of a CartThrob 10 download account page. It assumes the Product channel has an ExpressionEngine File or CartThrob Download field named product_download.

The field validates where the file comes from. The surrounding query establishes that the current member purchased the item. The download form adds a second member check when the form is submitted.

Where to store the file

CartThrob reads the download source from a product field. Pick the field by where the file lives:

  • ExpressionEngine File field — the best default. Editors choose the file from File Manager, and it keeps ExpressionEngine’s managed delivery for local or cloud (adapter-backed) storage. Nothing else to configure.
  • CartThrob Download field — when an editor must enter the source directly instead of picking from File Manager: a managed-file reference, a server file path, or an HTTP(S) URL. It validates the value when the entry is saved, so a product only ever points at an approved source. (A plain Text field can play the same role if you list it in cartthrob_download_fields; its values are validated the same way.)
  • Off-site or private files need a little configuration. A file on another host is allowed once you approve its exact origin, and a private file on your server must sit inside an approved download root. Set those in Configuration Overrides.

In every case the field validates the source, not whether this customer may have it — so keep the download form inside your purchase and member checks, as below. See the CartThrob Download fieldtype for the full field reference.

{if logged_out}
    <p>Please sign in to view your downloads.</p>
{/if}

{if logged_in}
    <h1>Your downloads</h1>

    {exp:cartthrob:order_items member_id="CURRENT_USER"}
        <article>
            <h2>{title}</h2>

            {exp:cartthrob:download_file_form
                field="product_download"
                entry_id="{item:entry_id}"
                member_id="CURRENT_USER"}
                <button type="submit">Download</button>
            {/exp:cartthrob:download_file_form}
        </article>

        {if no_results}
            <p>You do not have any downloadable purchases.</p>
        {/if}
    {/exp:cartthrob:order_items}
{/if}

For a reusable link, replace the form with get_download_link, but retain both the purchase query and member_id="CURRENT_USER":

<a href="{exp:cartthrob:get_download_link
    field='product_download'
    entry_id='{item:entry_id}'
    member_id='CURRENT_USER'}">Download</a>

For the download tags themselves, see Downloadable products and download_file_form.