PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 7, 2022

[FIXED] Add multiple custom columns to WooCommerce "My account" orders table

 February 07, 2022     hook-woocommerce, orders, php, woocommerce, wordpress     No comments   

Issue

I am trying to add a few columns to the WooCommerce > my account > orders page.

So far the code below adds more than one column but the content of the columns are not showing, where I'm I getting it wrong?

add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
    
     unset($columns['order-total']);
   $columns['custom-column'] = __( 'New Column', 'woocommerce' );
    $columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );

    return $columns;
}

add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
    // Example with a custom field

    if( $columns == 'custom-column' ) {

        echo 'Hello';

    }

    if( $columns == 'custom-column2' ) {

        echo 'Hello 2';

    }
} 

Solution

The woocommerce_account_orders_columns filter hook allows us to add 1 or more columns

// Add new column(s) to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
    $columns['custom-column'] = __( 'New Column 1', 'woocommerce' );
    $columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );

    return $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );

However, adding content per column is done via the woocommerce_my_account_my_orders_column_{$column_id} action hook.

So {$column_id} need to be replaced by the column key slug (custom-column or custom-column2) in this particular case. It is therefore not necessary to determine the correct column in the callback function via an if condition

So you get:

// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column( $order ) {    
    echo 'New Column 1';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'filter_woocommerce_my_account_my_orders_column_custom_column', 10, 1 );

// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column2( $order ) {    
    echo 'New Column 2';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column2', 'filter_woocommerce_my_account_my_orders_column_custom_column2', 10, 1 );


Answered By - 7uc1f3r
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing