Add Column in My Account > Orders to Show Product Name – WooCommerce
[May 2024]
A Tutorial with Full Code
At times your customers benefit from seeing the Product Name on their My Account > Orders screen rather than clicking into the order to see what was purchased.
In this example, I go through the steps to add the code to your WordPress functions.php to show a column named “Purchased” in the Orders Table.
Step 1:
This is actually a pretty straight forward process. You will only need to add the code below to your functions.php. We use a plugin such as php insterter or a child theme to do this.
However, if you enjoy reading the details, please see the specifics of the code below!
- Adding a New Column:
- We’re adding a new column to the table where customers can see their orders.
- This new column will show the products they bought.
- Function
filter_woocommerce_account_orders_columns
:- This function changes how the columns in the order table are displayed.
- It makes room for the new column.
- If it finds the column for order status, it puts the new column right next to it.
- Hooking the
filter_woocommerce_account_orders_columns
function:- We’re telling WooCommerce to use our function to adjust the order table columns.
- Function
action_woocommerce_my_account_my_orders_column_order
:- This function decides what goes into the new column.
- It goes through each item in an order.
- For each item, it gets the product and checks if it’s valid.
- If it is, it puts a link to the product’s page in the new column.
- Hooking the
action_woocommerce_my_account_my_orders_column_order
function:- We’re telling WooCommerce to use our function to fill in the new column with product information.
The full code that we are utilizing in functions.php is below:
// 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 ) {
// The WC_Product object
$product = wc_get_product( $item['product_id'] );
// 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 );