I am trying to add extra data in the Woo-Commerce report for Orders. I want to add two custom user field and product type column in the report so that one can download the CSV with the newly added data.
WHAT I HAVE TRIED YET
While researching for the issue, I found this article by Woo-Commerce developers. While going through this article I was able to add custom columns in my orders report but I am unsure on how do I push data to reportTableData variable which would eventually be reflected on the table.
My code to add column in order report of woocommerce is:
const addTableColumn = reportTableData => {
if ('orders' !== reportTableData.endpoint) {
return reportTableData;
}
const newHeaders = [{
label: 'Currency',
key: 'currency',
},
...reportTableData.headers,
];
const newRows = reportTableData.rows.map((row, index) => {
const item = reportTableData.items.data[index];
console.log(item);
const newRow = [{
display: item.currency,
value: item.currency,
},
...row,
];
return newRow;
});
reportTableData.headers = newHeaders;
reportTableData.rows = newRows;
return reportTableData;
};
addFilter('woocommerce_admin_report_table', 'dev-blog-example', addTableColumn);
I have copied this from the article and this code ads Currency column in the report table. I can add more column by adding item in newHeader.
I am unsure on how to get the data available, which I can use to map the data inside the row.
UPDATE #1 Console log of items is :
_links: Object { order: (1) […] }
customer_id: 1
customer_type: "new"
date_created: "2020-06-24 12:05:58"
date_created_gmt: "2020-06-24 12:05:58"
extended_info: Object { products: (1) […], coupons: (1) […], customer: {…} }
net_total: 0
num_items_sold: 1
order_id: 51
order_number: 51
parent_id: 0
status: "processing"
total_sales: 0