This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

How to Integrate Downloadable Products

LemonStand supports downloadable products out of the box. The standard simplest implementation is described in the Order Details Page article. The default implementation supposes that a customer can download files belonging to purchased products after the order is paid, from the Order Details page. This article demonstrates some implementation tricks which you may find useful.

Automatic customer registration

As downloadable products are available only for logged-in customers, it makes sense to register new customers automatically during the checkout. The automatic customer registration feature is described in this article: Automatic customer registration.

Adding links to downloadable files to email notifications

The default compound email variable order_content, which outputs the order content table, does not output links to downloadable files. You can extend this email variable. Open System/Settings/Email Templates page and click the Manage compound variables button in the toolbar. Click the order_content variable. We are going to add a list of downloadable files after the product name, in the first column of the order item table.

Find output_product_name() method call and add the following code after it:

<? if ($item->product->files->count && $order->is_paid()): ?>
  <? foreach ($item->product->files as $file): ?>
    <br/><a href="<?= $file->download_url($order) ?>"><?= h($file->name) ?></a> (<?= $file->size_str ?>)
  <? endforeach ?>
<? endif ?>

This code produces a result similar to this:

The Shop_ProductFile::download_url() method used in the code creates links to the built-in file download URL /download_product_file. When this URL is accessed, LemonStand first checks whether the customer is logged in. If it finds that no customer is logged in, it redirects the browser to the Login page, passing the originally requested URL as a parameter to the login page. On the login page you can use the Cms_Controller::redirect_url() method to create the redirect hidden field. If this approach is implemented, the customer is redirected back to the originally requested page (file download) after logging into the store.

Implementing custom file download page

If the built-in file download script is not suitable for you needs, you can implement a custom page which will be used as an URL for downloading product files. More specifically, you can use a custom page if

  • You want to display a custom message if the customer is not logged in.
  • You need to redirect the browser to a custom Login page.
  • You need the product files to be downloadable by guest customers if they know the exact file download URL or you want to implement a custom authentication method.

Create a new page on the CMS/Page page and paste the following code to the Content field:

<?
  try
  {
    $mode = $this->request_param(2);
    
    if (!strlen($file_id = $this->request_param(0))) 
      die("File not found");

    if (!strlen($order_hash = $this->request_param(1))) 
      die("File not found");
    
    $order = Shop_Order::create()->find_by_order_hash($order_hash);
    if (!$order || !$order->is_paid()) // This condition checks whether the order exists and whether it is PAID
      die("File not found");

    /*
     * Customer authentication support start.
     * Add this section if customer authentication is requried.
     */

    if (!$this->customer)
    {
      echo sprintf('Please <a href="%s">login</a> to download the file.', root_url('login').'/'.$this->create_redirect_url());
      die();
    }

    if ($order->customer_id != $this->customer->id)
      die("File not found");
 
    /*
     * Customer authentication support end.
     */
     
    $order->output_product_file($file_id, $mode);
    
    die("File not found");
  }
  catch (Exception $ex)
  {
    echo $ex->getMessage();
  }
    
?>

Note that if you use a custom file downloading page, you should update the Shop_ProductFile::download_url() method calls in the email compound variable. The Shop_ProductFile::download_url() method accepts the custom download page URL in the third parameter, so if your custom page URL was /custom_file_download_page, the download link code would look like follows:

<a href="<?= $file->download_url($order, null, '/custom_file_download_page') ?>"><?= h($file->name) ?></a>

Creating a page containing a full list of downloadable files

The following page snippet displays links to all downloadable files purchased by a currently logged in customer. Note that the page requires a customer to be logged in, so the Access field on the Security tab should have Customers only value.

<ul>
  <?
    foreach ($this->customer->orders as $order):
      if (!$order->is_paid())
        continue;
        
      foreach ($order->items as $item):
        if (!$item->product || !$item->product->files->count)
          continue;
          
        foreach ($item->product->files as $file):
  ?>
        <li><a href="<?= $file->download_url($order) ?>"><?= h($file->name) ?></a> (<?= $file->size_str ?>)</li>
      <? endforeach ?>
    <? endforeach ?>
  <? endforeach ?>
</ul>

Simplify the checkout process

LemonStand allows to remove the Shipping Method step from the checkout of the cart contains only downloadable products or services. Please read the Simplifying the checkout process article for details.


Previous: Storing Cart Content After Customer Places Order
Return to Tips and Tricks