Skip to main content
  1. Posts/

Drupal 8 Commerce 2.x Order Messages

·216 words·2 mins·
Drupal Drupal Commerce Drupal Development

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

mymodule.commerce_log_categories.yml
#

Defines our log categories.  e.g.

example_categories:
  label: Example Categories
  entity_type: commerce_order

example_categories_two:
   label: Example Categories Two
   entity_type: commerce_order

mymodule.commerce_log_templates.yml
#

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

example_status_update
  category: example_categories
  label: 'Status Update'
  template: '<p>Status has been updated.</p>'
example_second_status_update
  category: example_categories  
  label: 'Second Status Update'
  template: '<p>Status two has been updated.</p>'
update_example_status
  category: example_categories_two
  label: 'Order Status Update'
  template: '<p> Order Update Status</p>'
update_example_status_two
  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
$log->save();

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

Michael Michalak
Author
Michael Michalak
Experienced Drupal consultant and Fractional CTO with over 13 years of expertise in backend development, optimization, and securing Drupal applications, specializing in custom module development, migrations, and enterprise integrations across various industries.