Admin Custom List Table Columns

SureCart provides the ability to customize the list table views in its admin pages—like Orders, Products, Invoices, and more—by adding custom columns. This can help you display additional information relevant to your store’s workflow, at-a-glance, without needing to open individual records.

Below is a simple, contrived example demonstrating how to register and display a custom column. You can use this as a starting point for your own custom development.


Overview

  • Hook into the correct filter: Use manage_{$page}_columns to register new columns in the list table.
  • Hook into the correct action: Use manage_{$page}_custom_column to output the data for each item in your new column.
  • Identify $page: The $page variable corresponds to the SureCart admin screen you want to modify (e.g., sc-orders, sc-products, sc-invoices, etc.). You can see this in the URL when you’re on the respective page.
  • Use the $data model: In the callback function that outputs column content, SureCart passes a model object ($data) with information about that row. You can directly display properties from this model or use its ID to fetch additional data from your database.

Example Code

<?php
/**
 * Plugin Name: SureCart Admin Table Column Example
 * Description: Example plugin showing how to add custom columns to a SureCart admin list table.
 */

add_action(
    'admin_init',
    function () {
        if ( empty( $_GET['page'] ) ) {
            return;
        }

        // Identify which SureCart page we're on, e.g., sc-orders, sc-products, sc-invoices.
        $page = sanitize_text_field( $_GET['page'] ) ?? '';

        if ( empty( $page ) ) {
            return;
        }

        // 1. Hook into the "manage_{$page}_columns" filter to register a new column.
        add_filter(
            'manage_' . $page . '_columns',
            function ( $columns ) {
                // Add a new column labeled "Metabox."
                $columns['metabox'] = 'Metabox';
                return $columns;
            }
        );

        // 2. Hook into the "manage_{$page}_custom_column" action to display content for our new column.
        add_action(
            'manage_' . $page . '_custom_column',
            function ( $column_name, $data ) {
                // $data is the model object for the current row in the table.
                if ( 'metabox' === $column_name ) {
                    // Here, we simply output the object's ID.
                    // Replace this with whatever you want to display from $data.
                    echo esc_html( $data->id );
                }
            },
            10,
            2
        );
    }
); 

How It Works

  1. Detect the Current SureCart Page
    We check $_GET['page'] to identify which SureCart admin page is being viewed. For example, if you see page=sc-orders in the URL, $page is sc-orders.

  2. Register a New Column
    The manage_{$page}_columns filter lets you modify the array of columns. In the example, we add a new column with the key metabox and label it “Metabox.”

  3. Output the Data
    The manage_{$page}_custom_column action runs for each row in the list table. You get access to:

    • $column_name: The column’s key (e.g., metabox).
    • $data: The item’s model object. SureCart passes in an object representing the Order, Product, Invoice, etc.

    Use $data->id (or any other property/method on the model) to output the information you need. You can also use this ID to fetch related data from the database.


Customizing Your Columns

  • Column Label: Change the label to something more descriptive, e.g., "Order Notes", "Product Info", etc.
  • Data Display: Instead of echoing $data->id, display other properties (e.g., $data->total, $data->name, etc.) or perform additional queries using $data->id.

Common Use Cases

  • Adding an “Order Notes” Column to quickly view notes added by customers or admins for each order.
  • Displaying Custom Product Fields if you’ve extended the product model with metadata.

Troubleshooting

  1. No Column Appearing:

    • Make sure the $page matches the screen’s query var (e.g., sc-orders, sc-products, etc.).
    • Confirm your code is executed at the right time. The example uses admin_init—but ensure nothing else is overriding or removing your filters.
  2. No Data Showing:

    • Check that the property you’re accessing (like $data->id) actually exists on the model. Dumping the $data object can help you understand its available properties.

Summary

By hooking into the manage_{$page}_columns filter and manage_{$page}_custom_column action, you can easily add and populate custom columns in SureCart’s admin list tables. This allows you to surface critical store information at a glance, tailor your workflow, and maintain a more efficient management experience.

Feel free to expand on the example above to pull in more detailed data, integrate with third-party APIs, or display any additional information relevant to your store’s operations.