Add value to custom column on customer’s orders page in woocommerce

Tags: , , , ,



I was trying to add new column to the orders page of the customer’s recent orders table, the new column should show the details of the product like product name & description.

Tried different solution, was fortunate to add column name but not able to add the value to the column.

Here is my code:

add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns' );
function new_orders_columns( $columns = array() ) {
// Hide the columns
if( isset($columns['order-total']) ) {
    unset( $columns['order-status'] );
    unset( $columns['order-total'] );
    unset( $columns['order-actions'] );
}
// Add new columns
//this is my custom column order details
$columns['order-detail'] = __( 'Details', 'woocommerce' );
$columns['order-status'] = __( 'Status', 'woocommerce' );
$columns['order-total'] = __( 'Total', 'woocommerce' );
$columns['order-actions'] = __( ' ');

return $columns;
}

Screenshot of the result: enter image description here

Answer

You are very near…

1) Insert a new column in a specific location (another way):

add_filter( 'woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1 );
function add_custom_account_orders_column( $columns ) {
    $ordered_columns = array();

    // Inserting a new column in a specific location
    $ordered_columns['order-number'] = $columns['order-number'];
    $ordered_columns['order-date'] = $columns['order-date'];
    $ordered_columns['order-details'] =  __( 'Details', 'woocommerce' ); // <== New column
    $ordered_columns['order-status'] = $columns['order-status'];
    $ordered_columns['order-total'] = $columns['order-total'];
    $ordered_columns['order-actions'] = $columns['order-actions'];

    return $ordered_columns;
}

Code goes in function.php file of your active child theme (or active theme).

tested and works.


2) Display data in this new column (editing myaccount/orders.php template):

You will need to override through your active child theme, the myaccount/orders.php WooCommerce template.

At line 56 (just after ‘order-number’ code) you will insert the following and you will be able to add some specific content:

<?php elseif ( 'order-details' === $column_id ) : ?>
     <?php 
         // HERE GOES YOUR CUSTOM CODE (DISPLAYED CONTENT)
     ?>

You can use the available WC_Order object $order


How to get WooCommerce order details



Source: stackoverflow