1

I need to add and/or change an attribute in the order table on /admin/commerce/orders.

The Order Element itself does not have a EVENT_SET_TABLE_ATTRIBUTE_HTML, and I've tried with variations like this:

public function init()
{
    parent::init();
    Event::on(Element::class, Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,[$this, 'setOrderStatusTableAttributeHtml']);
}

public function setOrderStatusTableAttributeHtml($event, $attribute)
{
    if ($event->element instanceof \craft\commerce\elements\Order && $attribute == 'orderStatus') {
        return 'Changed';
    }
}

The event does not show up in the debug bar. I've also tried a EVENT_INIT with die('died') an that kills the execution, so my plugin is installed and loading.

1

I solved this by overriding the routes set by craft commerce and extend the OrderController and the Order element.

This is not a good solution (I'm going upload this as a plugin) and if several plugins overwrite the same route, only the latest plugin will work.

If anyone have a proper solution I'll accept that. I'll leave this answer here in the meantime.

Plugin::init()

    Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) {
        $event->rules['commerce/orders'] = 'commerce-easy-order-management/orders/order-index';
        $event->rules['commerce/orders/<orderId:\d+>'] = 'commerce-easy-order-management/orders/edit-order';
        $event->rules['commerce/orders/action/<orderId:\d+>'] = 'commerce-easy-order-management/orders/order-action';
    });

controllers\OrdersController

class OrdersController extends \craft\commerce\controllers\OrdersController
{
    /**
     * Index of Orders
     * Overwriting commerce/controllers/OrderController::actionOrderIndex()
     *
     * @return Response
     * @throws \Throwable
     */
    public function actionOrderIndex(): Response
    {
        // Remove all incomplete carts older than a certain date in config.
        Plugin::getInstance()->getCarts()->purgeIncompleteCarts();

        Craft::$app->view->registerCssFile('https://use.fontawesome.com/releases/v5.3.1/css/all.css');

        return $this->renderTemplate('commerce-easy-order-management/orders/_index');
    }
}

elements\Order

class Order extends \craft\commerce\elements\Order
{
    /**
     * @inheritdoc
     */
    public function getTableAttributeHtml(string $attribute): string
    {
        if($attribute !== 'orderActions') return parent::getTableAttributeHtml($attribute);
        return 'MyActionHTML';
    }

    /**
     * @inheritdoc
     */
    protected static function defineTableAttributes(): array
    {
        $attributes = parent::defineTableAttributes();
        $attributes['orderActions'] = ['label' => Craft::t('commerce', 'Actions')];
        return $attributes;
    }
}

templates/orders/_index.html

{% extends "_layouts/elementindex" %}

{% set title = "Orders"|t('commerce') %}
{% set docTitle = title~' - '~'Commerce' %}
{% set elementType = 'MY_NAMESPACE\\elements\\Order' %} 
{% set selectedTab = 'orders' %}
{% set selectedSubnavItem = "orders" %}
{% set bodyClass = (bodyClass is defined ? bodyClass~' ' : '') ~ "commerceorders     commerceordersindex" %}

{% do craft.app.view.registerTranslations('commerce', [
    "Total Revenue",
    "Last 7 days",
    "Last 30 days",
    "Last Week",
    "Last Month",
    "Custom Range",
    "Apply"
]) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.