Showing custom information in the order view in the dashboard

Just wrote this up in response to a support ticket, so figured I’d quickly post it here for future searchability. If you store custom information with an order, you may want to show that in the dashboard as well, and here’s one way to do that with a module in Commerce 0.11.


    public function initialize(EventDispatcher $dispatcher)
    {
        $dispatcher->addListener(\Commerce::EVENT_DASHBOARD_PAGE_BEFORE_GENERATE, array($this, 'addInfoToOrder'));
    }

    public function addInfoToOrder(PageEvent $event)
    {
        $page = $event->getPage();
        if ($page->key === 'order') {
            $orderId = $page->getOption('order');
            $order = $this->adapter->getObject('comOrder', ['id' => $orderId]);

            if ($order instanceof \comOrder) {
                $value = $order->getProperty('my_value', 'Some default value');
                $section = new SimpleSection($this->commerce);
                $section->priority = 7;
                $section->addWidget(new HtmlWidget($this->commerce, [
                    'html' => '<p><b>Some Value: </b>' . $value . '</p>'
                ]));
                $page->addSection($section);
            }
        }
    }