I Have a commerce site where I sell subscription using Commerce License, I want to send email notification to users that have licenses expire soon but I couldn't do it by rules.
I'm trying to do that by creating a custom module even if my drupal coding knowledge are very limited.
I have 2 codes sinppet, one for notify users when license expires, and the second is for send email using message notify module cause I guess it's easer than drupal mail system.
I don't know what variables I should use to have the licences owners user ID to combine my 2 codes.

Explain me please.

This is my custom module and the code works well for me

 * Implements hook_cron().
function license_notif_cron() {
  $expires = strtotime('-1 day');
  $query = new EntityFieldQuery();
    ->entityCondition('entity_type', 'commerce_license')
    ->propertyCondition('status', COMMERCE_LICENSE_ACTIVE)
    ->propertyCondition('expires', 0, '<>')
    ->propertyCondition('expires', $expires, '>');
  $results = $query->execute();
  if (!empty($results['commerce_license'])) {
    $license_ids = array_keys($results['commerce_license']);
    drupal_set_message('licensce expires soon');
    //send message

Sending message example -I'm not intersted in node_insert-, I already created a new message type called licence_expire_soon.

 * Implements hook_node_insert().
function foo_node_insert($node) {

  $message = message_create('foo_message_type', array('uid' => $node->uid));

  $wrapper = entity_metadata_wrapper('message', $message);

  $options = array(
     'rendered fields' => array(
       'message_notify_email_subject' => 'field_rendered_subject',
       'message_notify_email_body' => 'field_rendered_body',

  message_notify_send_message($message, $options);
  • are you interested in an answer to make this work with Rules anyhow? With zero custom code? – Pierre.Vriens Aug 13 '17 at 12:17

To glue the bits you have together you need to relate your to-be-expiring-soon license with a uid which can feed into message_create. So something like this in your hook_cron call...

//...after assigning $license_ids

foreach($license_ids as $license_id) {

   //...@TODO: checks on previous alert messages

   $uid = db_select('commerce_license','cl')
     ->fields('cl', array('uid'))
     ->condition('license_id', $license_id)

   //...@TODO: code for executing message_notify_send_message and sending message to $uid
  • hello @Shawn please check the code codeshare.io/f2rCx, it doesn't work for me – learner123 May 4 '15 at 16:44
  • That isn't complete code. The @TODO comments are places where you need to fill in bits. The important part is db_select query that should let you relate a uid with commerce license id. Let me know if you're not retrieving the $uid. – Shawn Conn May 4 '15 at 17:21
  • as you see in codeshare.io/f2rCx I filled the comments by the message sending code.mybee I didn't put the good variable for uid. – learner123 May 4 '15 at 17:36
  • If you're getting back a valid $uid, you should check your code for sending a message (I'm not familiar with message notify; I assumed that part was working). – Shawn Conn May 5 '15 at 2:06

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.