Woocommerce-admin: Extension example "table-column" No inclusion of new column in CSV export

Created on 28 Apr 2020  路  2Comments  路  Source: woocommerce/woocommerce-admin

In the extension example "table_column", the new column "Rating" does not export to CSV.

File: woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php

/**
 * When adding fields to a REST API response, we need to update the schema
 * to reflect these changes. Not only does this let API consumers know of
 * the new fields, it also adds them to Report Exports.
 *
 * @param array $properties The endpoint item schema properties.
 * @return array Filtered schema.
 */
function add_product_extended_attributes_schema( $properties ) {
    $properties['extended_info']['average_rating'] = array(
        'type'        => 'number',
        'readonly'    => true,
        'context'     => array( 'view', 'edit' ),
        'description' => 'Average product rating.',
    );

    return $properties;
}

... the description implies that this code will result in the new column being exported to CSV. This does not happen in my tests.

Expected behaviour: "Ratings" added as new column to CSV export.

Screenshot: https://www.dropbox.com/s/6kla5a33fntvd6v/2020-04-28-ratings.png?dl=0

extensibility

Most helpful comment

Thanks for your notes on this issue @EdithAllison! It does not look like there's a filter we can hook into currently, but I do agree there should be one.

The above looks like it will need the newly added filter in 2 places (one around each return).

The value looks like it should be easily mapped if already included with the API and if not, there are 2 options to include values further (a filter using "woocommerce_export_{$this->export_type}_column_{$column_name}" or a callable method get_column_value_{$column_name}. I don't believe any further work there will be needed to get this running.

Estimate: 1

All 2 comments

Okay, I understand this better now. The schema is only used as fallback option, so if an export is defined (as it is for Products), the schema is not used.

Query: is there a filter with which I can added columns for export? Function appears to be get_export_columns() but I can't see a filter that would hook into it.

File: /woocommerce-admin/src/API/Reports/Products/Controller.php

    /**
     * Get the column names for export.
     *
     * @return array Key value pair of Column ID => Label.
     */
    public function get_export_columns() {
        $export_columns = array(
            'product_name' => __( 'Product Title', 'woocommerce-admin' ),
            'sku'          => __( 'SKU', 'woocommerce-admin' ),
            'items_sold'   => __( 'Items Sold', 'woocommerce-admin' ),
            'net_revenue'  => __( 'N. Revenue', 'woocommerce-admin' ),
            'orders_count' => __( 'Orders', 'woocommerce-admin' ),
            'product_cat'  => __( 'Category', 'woocommerce-admin' ),
            'variations'   => __( 'Variations', 'woocommerce-admin' ),
        );

        if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
            $export_columns['stock_status'] = __( 'Status', 'woocommerce-admin' );
            $export_columns['stock']        = __( 'Stock', 'woocommerce-admin' );
        }

        return $export_columns;
    }

File /woocommerce-admin/src/ReportsCSVExporter.php

    /**
     * Get the report columns from the controller.
     *
     * @return array Array of report column names.
     */
    protected function get_report_columns() {
        // Default to the report's defined export columns.
        if ( $this->controller instanceof ExportableInterface ) {
            return $this->controller->get_export_columns();
        }

        // Fallback to generating columns from the report schema.
        $report_columns = array();
        $report_schema  = $this->controller->get_item_schema();

        if ( isset( $report_schema['properties'] ) ) {
            foreach ( $report_schema['properties'] as $column_name => $column_info ) {
                // Expand extended info columns into export.
                if ( 'extended_info' === $column_name ) {
                    // Remove columns with questionable CSV values, like markup.
                    $extended_info  = array_diff( array_keys( $column_info ), array( 'image' ) );
                    $report_columns = array_merge( $report_columns, $extended_info );
                } else {
                    $report_columns[] = $column_name;
                }
            }
        }

        return $report_columns;
    }

Thanks for your notes on this issue @EdithAllison! It does not look like there's a filter we can hook into currently, but I do agree there should be one.

The above looks like it will need the newly added filter in 2 places (one around each return).

The value looks like it should be easily mapped if already included with the API and if not, there are 2 options to include values further (a filter using "woocommerce_export_{$this->export_type}_column_{$column_name}" or a callable method get_column_value_{$column_name}. I don't believe any further work there will be needed to get this running.

Estimate: 1

Was this page helpful?
0 / 5 - 0 ratings