Commerce Commerce 1.x Developer Modules Examples Add Order View button

This example shows how you can add additional action buttons to the top of the order detail view. In this particular example it adds a link to edit the coupon the customer used in the order.

Other use cases could include adding a “Print” button that links to a different page that is formatted as a printable order receipt/invoice/packing slip, or adding a refund button.

<?php

namespace modmore\Commerce\Modules\Admin;

use modmore\Commerce\Events\Admin\OrderActions;
use modmore\Commerce\Modules\BaseModule;
use modmore\Commerce\Dispatcher\EventDispatcher;
// For modules that need to support 1.2 or before, replace with:
// use Symfony\Component\EventDispatcher\EventDispatcher;
// Please note that is DEPRECATED and will be REMOVED in 2.0

class AddOrderButton extends BaseModule
{
    public function getName()
    {
        return 'Add Coupon Button';
    }

    public function getAuthor()
    {
        return 'Mark Hamstra';
    }

    public function getDescription()
    {
        return 'Adds a button to the order detail view to manage the used coupon.';
    }

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

    public function addButton(OrderActions $event)
    {
        $order = $event->getOrder();
        if ($coupon = $order->hasCoupon()) {
            $actions = $event->getActions();
            $actions[] = [
                'url' => $this->adapter->makeAdminUrl('coupons/update', ['id' => $coupon->get('id')]),
                'title' => 'Edit coupon used in order',
                'modal' => true
            ];
            $event->setActions($actions);
        }
    }
}