2

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
abdul
  • 133
  • 3
  • 13

3 Answers3

2

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

abdul
  • 133
  • 3
  • 13
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
1

Accepted answer is only partially correct. Adding the column via filter is fine, but populating the column is better done via a action hook. This is clean and matches best with column being added via filter. While editing the orders.php page via child theme works, it will lead to future effort to maintain after updates to woocommerce change this page making child theme version out of date.

To add your content to the column create an action hook along side the filter in functions.php. (or better yet in a plugin!)

add_action( 'woocommerce_my_account_my_orders_column_order-detail' , 'your_function_name2',10,1 ); 
//'woocommerce_my_account_my_orders_column_<column_id>'
function your_function_name2( $order ) {

        // do stuff, ex: get_post_meta( $post->ID, 'key', true );

}

add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns',10,1 );
function new_orders_columns( $columns) {

    // Add new column. Kept simple to play nice with other filters.
    $columns['order-detail'] = __( 'Details', 'woocommerce' );   

    return $columns;
}

see this similar question, best answer by Misha

Stephen
  • 26
  • 6
0

Try to use this code

<?php
// ------------------
// 1. Register new endpoint to use for My Account page
// Note: Resave Permalinks or it will give 404 error

function bbloomer_add_premium_support_endpoint() {
  add_rewrite_endpoint( 'premium-support', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'bbloomer_add_premium_support_endpoint' );


// ------------------
// 2. Add new query var

function bbloomer_premium_support_query_vars( $vars ) {
  $vars[] = 'premium-support';
  return $vars;
}

add_filter( 'query_vars', 'bbloomer_premium_support_query_vars', 0 );


// ------------------
// 3. Insert the new endpoint into the My Account menu

function bbloomer_add_premium_support_link_my_account( $items ) {
  $items['premium-support'] = 'Premium Support';
  return $items;
}

add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_premium_support_link_my_account' );


// ------------------
// 4. Add content to the new endpoint

function bbloomer_premium_support_content() {
  echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>';
  echo do_shortcode( ' /* your shortcode here */ ' );
}

add_action( 'woocommerce_account_premium-support_endpoint', 'bbloomer_premium_support_content' );
Mykyta Dudariev
  • 462
  • 5
  • 17
  • thanks nikita but I was looking to add the product details of the order to the newly created column(Details). @nikita – abdul Jan 29 '18 at 11:16