I’m trying to add a column to display the row number in the product table. I added a column via this snippet:
add_action('woocommerce_admin_order_item_headers', 'add_number_of_row_order_items_header');
function add_number_of_row_order_items_header() {
echo '<th> NO. </th>';
}
function get_number_of_row_item($_product, $item, $item_id = null) {
$value = 1; // how can i get current NO. of row?
echo '<td>' . $value . '</td>';
}
add_action('woocommerce_admin_order_item_values', 'get_number_of_row_item', 10, 3);
But don’t know how to get the row number dynamically? In my current code, the number is hard coded, which is obviously not the intention. Any advice?
Update
I found the a way to add row number via JS/JQuery:
$('.woocommerce_order_items > thead > tr').prepend('<td style="background: #f8f8f8;padding: 18px;">NO.</td>');
var x = $('.woocommerce_order_items > tbody > tr').length;
for (let i = 1; i < x ; i++){
$(`.woocommerce_order_items > tbody > tr:nth-child(${i})`).prepend(`<td class='order-counter'>${i}</td>`);
}
$('tr.shipping td.order-counter').text('')
2
Answers
You can use it like this:
By default, no row numbers are supplied in the hook you use and since the hook is called row by row, it is useless to add a counter variable in the hook itself as it will be overwritten on every call.
So you will have to provide these yourself, this can be done on the basis of a global variable.
So you get: