Home > Blockchain >  Add row number column for order line items on WooCommerce admin order details page
Add row number column for order line items on WooCommerce admin order details page

Time:08-01

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('')

CodePudding user response:

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:

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
    // Set the column name
    $column_name = __( 'NO.', 'woocommerce' );
    
    // Display the column name
    echo '<th >' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );

// Add content
function action_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
    if ( method_exists( $item, 'is_type' ) ) {
        // Only for "line_item" items type, to avoid errors
        if ( ! $item->is_type( 'line_item' ) ) return;

        echo '<td>' . get_number() . '</td>';   
    }     
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

// Custom function
function get_number() {
    // NOT isset (counter)
    if ( ! isset( $GLOBALS['number'] ) ) {
        $GLOBALS['number'] = 1;
    } else {
        // Plus 1
        $GLOBALS['number']  = 1;
    }

    return $GLOBALS['number'];
}

CodePudding user response:

You can use it like this:

function my_woocommerce_admin_order_item_headers() {
    $column_name = 'TEXT';
    echo '<th>' . $column_name . '</th>';
}

add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {

    $value = 50;
if($item['type']=="line_item")
     echo '<td>' . $value . '</td>';
}
  • Related