Send message notification when license expired

Drupal Answers Asked by learner123 on November 15, 2021

I have a commerce site where I sell subscription using the Commerce License module. I want to send email notification to users whose licenses will expire soon.

I’m trying to do that by creating a custom module. I have 2 code snippets, one to notify users when the license expires, the second to send email using the Message Notify module.
I don’t know what variables I should use to have the licence owner’s.

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

This is an example of sending a message. I am not interested in hook_node_insert(); I already created a new message type (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);

One Answer

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) {

   //[email protected]O: checks on previous alert messages

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

   //[email protected]: code for executing message_notify_send_message and sending message to $uid

Answered by Shawn Conn on November 15, 2021

Add your own answers!

Related Questions

How to programmatically reference entities?

1  Asked on October 26, 2021 by sveggen


custom action plugin with ConfigurationForm

1  Asked on October 26, 2021


grouped paragraphs by field not filtering to current language

1  Asked on October 26, 2021 by turpentyne


How do I migrate node entity references?

0  Asked on October 26, 2021 by alexandre


Form textfield AJAX validation “deletes” injected services to form

1  Asked on October 26, 2021 by pedro-tentugal


Allowing anonymous users to contact node authors

1  Asked on October 26, 2021 by aroopk


How to cache a view for all users of a role?

1  Asked on October 26, 2021 by ktrev


Is it feasible to migrate by only migrating content?

2  Asked on October 26, 2021 by benjen


Get the breadcrumbs for a specific node other than the current

1  Asked on October 26, 2021 by nils-riedemann


Views exposed filters without “Apply” button

3  Asked on October 26, 2021


Saving form data and running custom queries in mysql

1  Asked on October 26, 2021 by benn-moffat


Add additional fields to the postal address

1  Asked on October 26, 2021 by jay-haase


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP