TransWikia.com

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();
      $query
        ->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);
  $wrapper->field_node_ref->set($node);
    
  $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) {

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

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

   //...@TODO: code for executing message_notify_send_message and sending message to $uid
}

Answered by Shawn Conn on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

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