← Back to Blog
Developers

How to Build a Custom Dashboard Widget for Your Store Admin

A custom dashboard widget on ShopsWired is part of a plugin: declare it in manifest.json, export a fetch(ctx) function that reads store data through the sw.* bridges, and return HTML rendered from a Liquid template. The admin then lists it in the dashboard gallery next to the built-in Revenue and Orders cards, and the merchant drags it onto any dashboard. There is no client SDK and no build step — it is the same server-side JavaScript you already write for routes and hooks.

The built-in widgets cover the numbers every store has. A plugin widget is for the number only your store cares about: the orders a warehouse is behind on, the reviews waiting for approval, the stock a supplier has promised. We'll build the first one — a card that lists paid orders still waiting to ship, oldest first, and flags any that have waited longer than a threshold the merchant sets.

What you need

Step 1: declare the widget in the manifest

A widget is declared twice in manifest.json: a scripts[] entry with "type": "widget" that maps a file to a widget id, and a widgets[] entry that describes the card in the gallery.

{
    "scripts": [
        {
            "path": "widgets/to-ship.js",
            "type": "widget",
            "widget_id": "to-ship"
        }
    ],
    "widgets": [
        {
            "id": "to-ship",
            "name": "Waiting to Ship",
            "icon": "📦",
            "description": "Paid orders that haven't shipped yet, oldest first.",
            "source": {
                "type": "internal",
                "script": "widgets/to-ship.js"
            },
            "permissions": ["Reads: orders"],
            "config_defs": [
                { "key": "warn_days", "type": "number", "label": "Flag orders older than (days)", "default": 2 }
            ],
            "defaults": {
                "width": "1/2",
                "rows": 4,
                "config": { "warn_days": 2 }
            },
            "placement": {
                "dashboard": true,
                "page": { "menu": true, "group": "Fulfillment" }
            }
        }
    ]
}

A few fields carry more weight than they look:

Step 2: write fetch(ctx)

The widget script exports fetch(ctx), exactly like a route handler. It runs on the server each time the card renders, with the widget's own state under ctx.widget. Put this in widgets/to-ship.js:

// widgets/to-ship.js
module.exports.fetch = function (ctx) {
    const warnDays = (ctx.widget.config && ctx.widget.config.warn_days) || 2;
    const dayMs = 24 * 60 * 60 * 1000;
    const now = Date.now();

    // Paid, not yet shipped. status is indexed, so this filter is cheap.
    const page = sw.orders.list({ filters: { status: "processing" }, limit: 100 });

    const orders = page.items
        .filter(function (o) { return !o.digital_only; })
        .map(function (o) {
            const age = Math.floor((now - new Date(o.created).getTime()) / dayMs);
            return {
                number: o.number,
                customer: (o.customer && o.customer.name) || "Guest",
                total: o.totals.total,
                currency: o.currency || ctx.widget.shop.currency,
                age_days: age,
                late: age >= warnDays
            };
        });

    orders.sort(function (a, b) { return b.age_days - a.age_days; });

    return sw.liquid.render("./widgets/to-ship.liquid", {
        orders: orders,
        late_count: orders.filter(function (o) { return o.late; }).length,
        warn_days: warnDays,
        more: !!page.cursor
    });
};

Why it's shaped this way:

Step 3: the Liquid template

The admin wraps your output in a body with the sw-widget class, which styles plain HTML to match the admin. Write semantic markup and skip the stylesheet. Put this in widgets/to-ship.liquid:

<header class="sw-row">
    <h3>Waiting to ship</h3>
    {% if late_count > 0 %}
        <strong style="color: var(--sw-danger)">{{ late_count }} over {{ warn_days }} days</strong>
    {% endif %}
</header>
{% if orders.size == 0 %}
    <p class="sw-muted">Nothing waiting. Every paid order has shipped.</p>
{% else %}
    <table>
        <tr><th>Order</th><th>Customer</th><th>Total</th><th>Waiting</th></tr>
        {% for o in orders %}
            <tr>
                <td>{{ o.number | escape }}</td>
                <td>{{ o.customer | escape }}</td>
                <td>{{ o.total | money_in: o.currency }}</td>
                <td>{% if o.late %}<strong style="color: var(--sw-danger)">{{ o.age_days }}d</strong>{% else %}{{ o.age_days }}d{% endif %}</td>
            </tr>
        {% endfor %}
    </table>
    {% if more %}<p class="sw-muted">Showing the first 100.</p>{% endif %}
{% endif %}

Two details. The customer name is whatever the shopper typed, so it goes through | escape, and so does the order number for good measure — Liquid doesn't escape output for you. And the red comes from --sw-danger, one of the admin's design tokens, so the widget keeps matching the admin if its colors change.

That's the whole widget. Push the plugin, open a dashboard, and Waiting to Ship is in the gallery. You don't write a refresh button: the card's hover toolbar has one that re-runs fetch(ctx), and changing the threshold in the config panel reloads the card with the new value.

Taking it further

Where widgets fall short

The developer docs carry the full widget reference: every manifest field, the reactive sw-* attributes for interactive cards, the record and file pickers, and record-field editors for custom records. Start with one card that answers one question your team asks every morning — that's usually the widget that stays on the dashboard.