3

I'm using rules scheduler (on D7) to set up a reminder email based on a time specified within a commerce order (more specifically a line item in the order). I was able to get everything set up correctly using these instructions, so that the email is scheduled and send out. However, I am having a problem with the actual content of the email appearing correctly.

Just to clarify the rule setup, the flow is

  1. Line item is updated
  2. Rules scheduler schedules a component to run based on line item's date field
  3. When the scheduled time is hit, the component is triggered
    1. A view that serves the list of users is loaded
    2. Component loops through list of users in the view
      • Email is sent to each user

The content of the email is defined by another view, which I am embedding using:

<?php
$id = $line_item->line_item_id;
print views_embed_view('commerce_checkout_page_for_email_embed', 'block_4', $id);
?>

The $id above is used to pass the line item's ID into the view to filter results by the current line item.

In my testing, when triggering the component directly on Line Item Update instead of at a scheduled time, everything works perfectly. The email is sent, and the body of the email contains the proper view filtered by the current line item.

However, when the email is sent by rules scheduler, the body of the email is empty. When I hard code $id ($id=1776 instead of $id = $line_item->line_item_id) the body of the email is exactly as it is supposed to be. I can only assume that this means that for some reason, the scheduled rule is not able to access the variable I'm passing to it. I also tried passing the token for the line item id, but you cannot pass token in PHP, so that was a bust.

Does anybody know why this might be, or have any suggestions?

  • To get somewhere, you should put print_r($line_item->line_item_id) somewhere in your mail. This way you can see what your code receives as id. If it's just nothing, try to find out if the line item is available at all. If you're doing it all in php you may have to load the whole order first by using commerce_order_load() oder commerce_cart_order_load(). – upunkt Jun 19 '17 at 13:50
0

Instead of scheduling a rule to execute and send an email with no context, you could use commerce_messages to create a message entity with all the data saved and then schedule the message_notify rule to send an email.

Messages: https://www.drupal.org/project/message

Commerce Message: https://www.drupal.org/project/commerce_message

Message Notify: https://www.drupal.org/project/message_notify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.