Share » Learn » eZ Publish » Selling Pay-Per-Download Products

Selling Pay-Per-Download Products

Sunday 30 August 2009 3:00:00 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

We tested that our role assignment works by having our "Test User" account purchase a product and manually access the path to the download file node from the media library. However, this is not a reasonable use case. At the very least, we'd want to give the buyer links to download their purchases. And to be more user friendly, the links should lead directly to the downloadable file – not to an intermediary page.

Note: In this section, we will discuss modifying certain templates. However, when modifying any existing templates (from, for example, the Website Interface design or the standard design), it is best practice to create your own design extension containing copies that take precedence over the original templates.

Confirmation e-mail template

When a buyer has completed the checkout process, eZ Publish will e-mail the order details to the buyer and to the site administrator. Within this e-mail is an itemized list of purchases:

Product items

1x eZ Publish Advanced Content Management $15.00: $15.00

Subtotal of items: $15.00

Beneath each downloadable item, we will add a download link. The order e-mail template in question is located at design/base/templates/shop/orderemail.tpl. We will copy this file to our design extension in order to edit it. Currently, the code to loop through each purchase is as follows:

{section name=ProductItem loop=$order.product_items show=$order.product_items sequence=array(bglight,bgdark)}
{$ProductItem:item.item_count}x {$ProductItem:item.object_name} {$ProductItem:item.price_inc_vat|l10n( 'currency', $locale, $symbol )}: {$ProductItem:item.total_price_inc_vat|l10n( 'currency', $locale, $symbol )}

{/section}

We will replace this code with this:

{foreach $order.product_items as $item}
{$item.item_count}x {$item.object_name} {$item.price_inc_vat|l10n( 'currency', $locale, $symbol )}: {$item.total_price_inc_vat|l10n( 'currency', $locale, $symbol )}
{def $download_classes = ezini('PayPerDownloadSettings', 'ContentClasses', 'payperdownload.ini')}
{if $download_classes|contains($item.item_object.contentobject.class_identifier)}
    {def $download_attributes = ezini('PayPerDownloadSettings', 'Attributes', 'payperdownload.ini')}
    {def $download_attribute = $download_attributes[$item.item_object.contentobject.class_identifier]}

    - Download: {concat('http://', ezini('SiteSettings', 'SiteURL', 'site.ini'), '/content/download/',$item.item_object.contentobject.data_map.$download_attribute.content.id,'/',$item.item_object.contentobject.data_map.$download_attribute.content.data_map.file.id)|ezurl('no')}
    {undef $download_attributes}
    {undef $download_attribute}
{/if}

{/foreach}

The code uses very similar logic to the workflow extension that we created in order to loop through each purchased product, see whether it is a downloadable product, and if so, access the node of the download file. Here, we are building a link to the "download" view of the "content" module, which is a special view that prompts the user through their browser to either download or save the file instead of showing a page. That view takes two parameters: the ID of the object in question, and the ID of the attribute containing the file. This is more efficient, user-friendly, and secure than linking to the page representing the download file's node in the media library. The e-mail text should now look something like this:

Product items

1x eZ Publish Advanced Content Management $15.00: $15.00
    - Download: http://www.yoursite.com/training/content/download/172/1028

Subtotal of items: $15.00

Order confirmation page

The same download link from the confirmation e-mail should also be given on the order confirmation page (the last page shown in the checkout process). The default file used, and to be overridden in a design extension, is located at extension/ezwebin/design/ezwebin/templates/shop/orderview.tpl. The existing code loops through each purchased product in a table:

{foreach $order.product_items as $product_item sequence array( 'bglight', 'bgdark' ) as $style}
<tr class="{$style}">
    <td>
    <a href={concat( "/content/view/full/", $product_item.node_id )|ezurl}>{$product_item.object_name}</a>
    </td>

We will add to that code to show the "content/download" links for each downloadable product:

{foreach $order.product_items as $product_item sequence array( 'bglight', 'bgdark' ) as $style}
<tr class="{$style}">
    <td>
    <a href={concat( "/content/view/full/", $product_item.node_id )|ezurl}>{$product_item.object_name}</a>
    {def $download_classes = ezini('PayPerDownloadSettings', 'ContentClasses', 'payperdownload.ini')}
    {if $download_classes|contains($product_item.item_object.contentobject.class_identifier)}
        {def $download_attributes = ezini('PayPerDownloadSettings', 'Attributes', 'payperdownload.ini')}
        {def $download_attribute = $download_attributes[$product_item.item_object.contentobject.class_identifier]}
        <br />

       - Download: <a
href={concat('content/download/',$product_item.item_object.contentobject.data_map.$download_attribute.content.id,'/',$product_item.item_object.contentobject.data_map.$download_attribute.content.data_map.file.id)|ezurl()}>{$product_item.item_object.contentobject.data_map.$download_attribute.content.name}</a>
        {undef $download_attributes}
        {undef $download_attribute}
    {/if}
    </td>

This will product a result similar to the screenshot below:

The pay-per-download solution is now complete!

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)