Halloween Sale 15% off Ends in See the plans
PHP

Recent Orders on the My Account Dashboard

Lists the customer's three latest orders with status and total on the My Account dashboard, so they see what matters without clicking through.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_recent_count = 3; // Orders to show.

add_action( 'woocommerce_account_dashboard', function () use ( $wpide_recent_count ) {
    $orders = wc_get_orders( array(
        'customer_id' => get_current_user_id(),
        'limit'       => $wpide_recent_count,
        'orderby'     => 'date',
        'order'       => 'DESC',
        'status'      => array_keys( wc_get_order_statuses() ),
    ) );

    if ( ! $orders ) {
        return;
    }

    echo '<h3>' . esc_html( 'Your recent orders' ) . '</h3><table class="woocommerce-orders-table shop_table shop_table_responsive"><thead><tr><th>' . esc_html( 'Order' ) . '</th><th>' . esc_html( 'Date' ) . '</th><th>' . esc_html( 'Status' ) . '</th><th>' . esc_html( 'Total' ) . '</th></tr></thead><tbody>';

    foreach ( $orders as $order ) {
        printf(
            '<tr><td data-title="%1$s"><a href="%2$s">#%3$s</a></td><td data-title="%4$s">%5$s</td><td data-title="%6$s">%7$s</td><td data-title="%8$s">%9$s</td></tr>',
            esc_attr( 'Order' ),
            esc_url( $order->get_view_order_url() ),
            esc_html( $order->get_order_number() ),
            esc_attr( 'Date' ),
            esc_html( wc_format_datetime( $order->get_date_created() ) ),
            esc_attr( 'Status' ),
            esc_html( wc_get_order_status_name( $order->get_status() ) ),
            esc_attr( 'Total' ),
            wp_kses_post( $order->get_formatted_order_total() )
        );
    }

    echo '</tbody></table>';
} );

Notes

Change $wpide_recent_count to show more or fewer orders.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.