Is there any way to add a token to an email sent by rules with a link to download the licensed file? Or even if there is another way using php or something to produce a link to download the licensed files from an email.

Any ideas here would be great!

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz’s picture

[commerce-license:access-details] should do the trick, try it.

deggertsen’s picture

Status: Active » Fixed

Yes, I think that's the token I needed. However, right now it's not linking to anything, it's just giving the name of the licensed file. I think this is because of my problem in #2066109: Disable SQL rewriting for the Files view. Assuming that is the case we can go ahead and close this issue.

Thanks!

bojanz’s picture

Category: support » bug
Status: Fixed » Active

I'm not convinced the issues are related.
I think the access limits are checked for the current user, which is not the same as the license owner at the point when the rule is invoked.
I guess we should disable the access limit check for the token. I will give it some more thought after dinner.

deggertsen’s picture

Yes, I was just coming back to this issue to confirm exactly what you said here. Unfortunately it simply states the file name without linking it to download the actual file. Seems to me that in most cases where this token is used access limit check should be disabled. That would work for me at least. I could see in some cases where people would want or need that to count towards their access limit though...

bojanz’s picture

Status: Active » Needs review
FileSize
599 bytes

Can you try this patch?

EDIT: And it might also make sense to show the links without the icons. What do you think is preferable?
EDIT2: Committed the patch in any case, since it makes sense.

deggertsen’s picture

Status: Needs review » Needs work

Tried it, but there is still no link, it just has the file icon and the name of the file. It doesn't link to anything though.

I've attached a screenshot of part of the email just to help with the visual.

deggertsen’s picture

FileSize
13.2 KB

Oops, here's the screenshot.

bojanz’s picture

Can you export your rule, or describe your setup? I have trouble reproducing.

deggertsen’s picture

Here is my rule export (personal site data removed):

{ "rules_send_email_with_license_link" : {
    "LABEL" : "Send email with license link",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_license" ],
    "ON" : [ "commerce_license_insert" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-license:status" ], "value" : "2" } },
      { "NOT user_is_blocked" : { "account" : [ "commerce-license:owner" ] } }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "commerce-license:owner:mail" ],
          "subject" : "[TSG] Get your file download now!",
          "message" : "You may have received a separate email with more details, but we wanted to make sure you knew that you have a download available!\r\n\r\nDownload: [commerce-license:access-details]\r\n\r\nYou can click here and login to download your file(s) through your account: https:\/\/example.com\/user\/[user:uid]\/my-files\r\n\r\nPlease \u003Ca href=\u0022https:\/\/example.com\/form\/contact\u0022\u003Econtact us\u003C\/a\u003E if you have any problems.\r\n\r\nWarmly,\r\n\r\nThe Example Team"
        }
      }
    ]
  }
}

Notice this is on the event of commerce_license_insert.

Thanks for looking at this.

bojanz’s picture

Did you manage to find a workaround for this?
I tried to finally debug this on the plane, reproduced it, but didn't get anywhere on determining the cause (though it seems that the bug is on the commerce_file side, no idea why it occurs).

deggertsen’s picture

Unfortunately I did not find a workaround. All I am doing now in the email that goes out is directing them to their "Files" page in their profile so that they can then click the link to download their purchased files. You would think that wouldn't be a big deal, but we get a significant amount of email from people still asking how they download their purchased files. It would be nice if this worked, but like you I have not been able to figure it out yet.

Daniel Wentsch’s picture

Issue summary: View changes

*Bump*

It's been a while, thought I'd ask if somebody has made any progress on this? It does not seem to be such an uncommon requirement and I didn't find a solution yet.

My specific use case is that I'd like to allow anonymous checkout for file purchases.

deggertsen’s picture

Nope, I'd still love to see this functionality, but I don't have any idea how to go about getting it to work and it's not high enough priority for us to pay someone else the big bucks to figure it out. Still in the same situation as I stated in #11.

torgosPizza’s picture

If it helps anyone I handled this in the Checkout page itself (nothing emailed) by creating a custom checkout pane. I basically copied the one that comes with Commerce License and added a loop for all licenses in an order, and showing those files if a license exists.

Here is some sample code, it should be correct but I took out some of our specific stuff so make sure you test it (and check my punctuation). To use it, create a custom module (in this example it's called MYMODULE.module) and then create a directory off the main module directory called "includes". Inside the includes/ directory create a file called commerce_checkout.checkout_pane.inc, and add the code below. Hack it to your tastes.

/**
 * Checkout pane callback: License completion message.
 *
 * Outputs a pane for each found license that includes download links.
 */
function MYMODULE_checkout_form($form, &$form_state, $checkout_pane, $order) {
  $pane_form = array(
    '#type' => 'container',
  );
  $balance = commerce_payment_order_balance($order);
  $order_paid = ($balance && $balance['amount'] <= 0);
  $js_attached = FALSE;

  // Show a pane for each found license (per product).
  $licenses = commerce_license_get_order_licenses($order);

  if (!empty($licenses)) {
    foreach ($licenses as $license) {
    $synchronizable = ($license instanceof CommerceLicenseSynchronizableInterface);
    
    // If this is a synchronizable license, attach the JS for the refresh.
    if (!$js_attached && $synchronizable) {
      $pane_form['#attached']['library'][] = array('system', 'drupal.ajax');
      $pane_form['#attached']['js'] = array(
        drupal_get_path('module', 'commerce_license') . '/commerce_license.js',
      );
      $js_attached = TRUE;
    }
    // Get the message to show.
    $message = t('Your license will be activated once your payment has been processed.');

    // If the order is paid, show them the licenses for each product
    if ($order_paid) {
      $product = $license->wrapper->product->value();

      $message = '<section class="checkout-product">'; 
      $message .= 'Download your purchased files.<br/><br/>';
      $message .= $license->accessDetails();
      $message .= '</section>'; 

      $element = array(
        '#type' => 'markup',
        'output' => array(
          '#markup' => $message,
        ),
      );

      $message = drupal_render($element);

      $title_arguments = array(
        '%product_title' => $license->wrapper->product->title->value(),
      )
      $pane_form[$license->license_id] = array(        
        '#type' => $checkout_pane['fieldset'] ? 'fieldset' : 'container',
        '#title' => t('Download %product_title:', $title_arguments),
        '#collapsible' => $checkout_pane['collapsible'],
        '#collapsed' => $checkout_pane['collapsed'],
        'output' => array(
          '#markup' => $message,
        ),
      );
    }
  }

  return $pane_form;
}

We don't really have a need for download links included in an email receipt but if this is something I'm able to drum up, I will post it here. Hopefully this helps someone who wants similar functionality, though.

nedjo’s picture

An issue to consider is that a download link in an email will potentially produce an access denied result in the case that a user isn't already logged in.

torgosPizza’s picture

That's true. In our experience (especially if a user purchases dozens of products, which does happen on our site) it's better to just provide links to the product itself and include a "Download" link somewhere on that page if a user has purchased it. That is a field handler I'm working on for the Commerce File module - a "purchased links" view that could be added to a Panels pane etc.

jkdev’s picture

The Reason you are not seeing the file name as link to download has something to do with this:

<?php
$can_download = commerce_file_can_download($variables['license'], $real_file);
?>

look at this file: commerce_file.module
at this function: theme_commerce_file_download_link

the $can_download variable apparently set to false. so this is a label rather than a link with token.

upunkt’s picture

Any update on this?

Actually, I'm not even seeing the unlinked text like @deggertsen. The [commerce-license:access-details] is converted to either

- nothing (anonymous user, logged in via Login Toboggan and given a role with full file access) or
- n.v. (User 1)

Download from the Checkout-complete view works, though.

To get somewhere I tried to send the token on the event "Commerce Order is viewed" and tried various mail mechanisms, Drupal's own and Commerce Email with and without Variable Email. No difference sending as HTML or Text. The license is active, sure.

I've tried to use OP's rule from #9, and that made no difference either.
I've been testing with the latest stable versions of Commerce File (7.x-2.0-beta3) and Commerce License (7.x-1.3) and their latest dev-versions also.

As I do not find any official documentation about [commerce-license:access-details] I wonder whether there might be certain conditions to be met before this token can be converted. Please, @bojanz, could you explain what makes this token usable?

Thanks a lot in advance.

EDIT:
Other than the token-suggestions imply, some of the tokens need to be prefixed with a site, eg.
[site:current-user:field_mycustomfield].

Note the underscore in field_mycustomfield. This technique doesn't help with the access details, though.

upunkt’s picture

I got it.

When configuring your rule for sending out e-mail you must react on an event from the Commerce License-block. Since all my other e-mails have their origin in a Commerce Order-event I tried adding the token [commerce-license:access-details] just there. This fails. Configure an extra rule.

For completeness, this is my setup.
Rules 7x.-2.9
Commerce Email 7.x-2.x-dev
Mime Mail 7.x-1.0-beta4
Variable Email 7.x-1.0-alpha1+3-dev
Commerce 7.x-1.13
Commerce File 7.x-2.0-beta3+3-dev
Commerce License 7.x-1.3+12-dev

Puh, many dev-versions. But I'm not willing to switch back to non-dev since everything works. Still, I encourage you to try with stable ones first.