I'm trying to figure out how to set a message using code?

The content of my mails are generated by another module but I want to use the Message and Message Notify to store the mails and send the mails, since it all fits in with the existing views/logs/history functionality.

But how can I set the message data properly in code?

I had a good look at the Examples in Message but I still miss some parts.

Case: I want to extend the "commerce_order_created" message type, so I added extra fields.

/**
 * Implements custom function().
 */
function MYMODULE_message_insert_status($order, $communication) {
  // Load data
  $account = user_load($order->uid);

  $message = message_create('commerce_order_created', array(), $account);
  $message->message_commerce_order[LANGUAGE_NONE][0]['target_id'] = $order->order_id;

  // Save reference to the order in the order reference field, and the
  $wrapper = entity_metadata_wrapper('message', $message);

  $now = time();
  $wrapper->field_confirmation_date_created->set($now); // Custom fields to extend the commerce_order_created message type
  $wrapper->field_confirmation_date_send->set($now); // Custom fields to extend the commerce_order_created message type

  // Save message
  $wrapper->save();
}

How can I set the data for?
Notify - Email subject
Notify - Email body

I loaded the message object and there these fields are not present?

Comments

thim’s picture

Anybody got some info to get me started?

asak’s picture

bump. me too. trying to set a Commerce Message.
1. Implemented the hook_default_message_type:

  $items['commerce_order_processed'] = entity_import('message_type', '{
    "name" : "commerce_order_processed",
    "description" : "Commerce Order: order has been processed",
    "argument_keys" : [ "@order_process_key_one", "@order_process_key_two" ],
    "argument" : [],
    "category" : "commerce_order_message",
    "data" : { "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" } },
    "language" : "",
    "arguments" : null,
    "message_order_display_name" : { "und" : [ { "value" : "Order", "format" : null } ] },
    "message_text" : { "und" : [
        {
          "value" : "<p>Processed order, info one: @order_process_key_one (info two: @order_process_key_two).</p>",
          "format" : "commerce_order_message"
        }
      ]
    },
    "rdf_mapping" : []
  }');

  return $items;

2. Created a function:

function mymodule_order_comment($order, $type, $msg) {
  $message = message_create('commerce_order_processed', array('arguments' => array('@order_process_key_one' => 'msg1', '@order_process_key_two' => 'msg 2')), $order);
  $message->message_commerce_order['und'][0]['target_id'] = $order->order_id;
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->message_commerce_body->set('test automesg...');
  $wrapper->save();
}

But - i can't seem to get the message attached to the order...
Any clues - highly appreciated ;)

-Asaph

5kot’s picture

Had the same problem and this is what I got working

global $user;

$order_id = 1; // your order id

$message = message_create('commerce_order_admin_comment', array('uid' => $user->uid));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->message_commerce_order = entity_load($order_id);
$wrapper->message_commerce_body->value = 'Comment to be submitted';

$message->save();
bluegeek9’s picture

Status: Active » Closed (outdated)