Rahul Kumar Yadav
Rahul Kumar Yadav

Reputation: 67

How to show product name in a new column in WooCommerce "My account" orders table

I have added a new column in recent order table. I want to show the product name in this column.

I am using below php code. Is it possible making some changes to this code to show the products name instead?

**
 * Adds a new column to the "My Orders" table in the account.
 *
 * @param string[] $columns the columns in the orders table
 * @return string[] updated columns
 */
function th_wc_add_my_account_orders_column( $columns ) {

    $new_columns = array();

    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add ship-to after order status column
        if ( 'order-status' === $key ) {
            $new_columns['new-data'] = __( 'New Data', 'textdomain' );
        }
    }

    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'th_wc_add_my_account_orders_column' );

 /**
 * Adds data to the custom "new-data" column in "My Account > Orders".
 *
 * @param \WC_Order $order the order object for the row
 */
function th_wc_my_orders_new_data_column( $order ) {

    $new_data = get_post_meta( $order->get_id(), 'new_data', true ); // Get custom order meta
    echo ! empty( $new_data ) ? $new_data : '–';
    
}
add_action( 'woocommerce_my_account_my_orders_column_new-data', 'th_wc_my_orders_new_data_column' );

Upvotes: 3

Views: 1887

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

The woocommerce_account_orders_columns filter lets us add or adjust the columns included in this list, so we can add a new one.

The woocommerce_my_account_my_orders_column_{$column_id} action fires to let you populate the column you just added with content.

  • The column ID will be the array key you’ve added in the previous step, order-products in this case

So to display the products that an order contains, you can use the following:

// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
    // Empty array
    $new_columns = array();

    // Loop trough existing columns
    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // Add after order status column
        if ( $key === 'order-status' ) {
            $new_columns['order-products'] = __( 'Products', 'woocommerce' );
        }
    }

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

// Adds data to the custom "order-products" column in "My Account > Orders"
function action_woocommerce_my_account_my_orders_column_order( $order ) {    
    // Loop through order items
    foreach ( $order->get_items() as $item_key => $item ) {
        // Get a an instance of product object related to the order item
        $product = $item->get_product();
        
        // Instanceof
        if ( $product instanceof WC_Product ) {
            // Output
            echo '<div class="product"><a href="' . $product->get_permalink() . '">' . $product->get_name() . '</a></div>';
        }
    }
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'action_woocommerce_my_account_my_orders_column_order', 10, 1 );

Result:

new-product-column

Upvotes: 4

Related Questions