Commerce entity's and storing arbitrary data

Published on Thursday 25, June 2015

Drupal Commerce entities all make use of the "data" attribute. The data attribute is a blob that contain just about anything you would like - for example, Commerce Shipping utilizes this for shipping line items. Recently there was a StackExchange question on the documentation of this attribute. The best way to learn about an entity (in Drupal 7) and how it operates is to review it's controller class, which each Commerce module has in its includes folder.

/**
 * Unserializes the data property of loaded orders.
 */
public function attachLoad(&$queried_orders, $revision_id = FALSE) {
  foreach ($queried_orders as $order_id => &$order) {
    $order->data = unserialize($order->data);
}

The database layer will always serialize items on save, but you're required to unserialize when returning data.