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); }