Drupal 8 Commerce 2.x Order Messages

Michalak Drupal Commerce Developer

I'm currently working on a custom feature for Drupal 8 Commerce 2.x, and I decided along the way that I wanted to update orders with messages. Documentation on this topic was so light that I decided to write my own post about it!

Your custom module will need to have a couple of additional files:
- mymodule.commerce_log_categories.yml
- mymodule.commerce_log_templates.yml


Defines our log categories.  e.g.

  label: Example Categories
  entity_type: commerce_order

   label: Example Categories Two
   entity_type: commerce_order


This file defines the messages or message templates that will be used and seen on orders.

  category: example_categories
  label: 'Status Update'
  template: '<p>Status has been updated.</p>'
  category: example_categories  
  label: 'Second Status Update'
  template: '<p>Status two has been updated.</p>'
  category: example_categories_two
  label: 'Order Status Update'
  template: '<p> Order Update Status</p>'
  category: example_categories_two
  label: 'Order Status Update Two'
  template: '<p> Order Update Status Two</p>'

Creating a Message on Commerce Orders

Now that we have our categories and message templates defined, we're ready to start adding messages to our orders.   

We will need to have the order entity:

$log_storage = \Drupal::entityTypeManager()->getStorage('commerce_log');
$log = $log_storage->generate($order, 'update_example_status_two');  //use one of our predefined templates

And that's it— now we can add messages to our orders in Drupal 8 Commerce 2.x. Go message it up!

Original Post: https://trail9.com/?q=news/drupal-8-commerce-2x-order-messages

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.